Configuration.EnvironmentVariablesFromFile 1.0.0

.NET Standard 2.1
dotnet add package Configuration.EnvironmentVariablesFromFile --version 1.0.0
NuGet\Install-Package Configuration.EnvironmentVariablesFromFile -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Configuration.EnvironmentVariablesFromFile" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Configuration.EnvironmentVariablesFromFile --version 1.0.0
#r "nuget: Configuration.EnvironmentVariablesFromFile, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Configuration.EnvironmentVariablesFromFile as a Cake Addin
#addin nuget:?package=Configuration.EnvironmentVariablesFromFile&version=1.0.0

// Install Configuration.EnvironmentVariablesFromFile as a Cake Tool
#tool nuget:?package=Configuration.EnvironmentVariablesFromFile&version=1.0.0

AddEnvironmentVariablesFromFile

This package contains new configuration provider to get values from file.

This provider is similar to AddEnvironmentVariables:


static void Main(string[] args)
{
	var cfg = InitOptions<AppConfig>();
	string server = cfg.Server;
	string userId = cfg.UserName;
	...
}

private static T InitOptions<T>() where T : new()
{
	var config = InitConfig();
	return config.Get<T>();
}

private static IConfigurationRoot InitConfig()
{
	var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
	var builder = new ConfigurationBuilder()
		.AddJsonFile($"appsettings.json", true, true)
		.AddJsonFile($"appsettings.{env}.json", true, true)
		.AddEnvironmentVariables();

	return builder.Build();
}

In this case if exist token in json file that exist in AppConfig class like a property, is populated with this value.

Adding new provider:

var builder = new ConfigurationBuilder()
	.AddJsonFile($"appsettings.json", true, true)
	.AddJsonFile($"appsettings.{env}.json", true, true)
	.AddEnvironmentVariables()
	.AddEnvironmentVariablesFromFile();

Provider searches environment variables ending with suffix _FILE (there is also a parameter in AddEnvironmentVariablesFromFile method to change it) and the content will be used as the file name. Then the contents of this file are read and entered as a value.

In previous example, AppConfig class contains property Server and if Environoment Variable contains:

  • Server_FILE = /tmp/server.txt

Provider read the content of this file, and insert value into Server property.

This is useful in Docker containers or Kubernetes for passing parameters like secrets or config.

License

MIT ©

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 314 8/16/2021