I want my .Net Core 5 console application to select settings from the appropriate appsettings file based on the DOTNET_ENVIRONMENT environment variable. I'm testing this by running it in the Visual Studio 2019 debugger and fetching the environment from my launchSettings.json file.
In a .Net Core 5 console application I have 4 "appsettings" files:
- appsettings.json
- appsettings.Development.json
- appsettings.Staging.json
- appsettings.Production.json
Each file Properties is set to Build Action : Content, and Copy to Output Directory: Copy if newer.
In my launchSettings.json I have my environment set to "Staging" like so:
{
"profiles": {
"MyAppName": {
"commandName": "Project",
"dotnetRunMessages": "true",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Staging"
}
}
}
}
I need access to my configuration in the "Main" method in Program.cs, so in that class I am setting a module-level string variable "_environment" like so in the static constructor:
_environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
This works; the value "Staging" gets loaded into the variable _environment.
I then load my Configuration into a static variable like so: (EDIT--this was my mistake, assuming this static property loaded AFTER the static ctor. In fact it loaded BEFORE the static ctor. This meant the _environment variable was not set, which means my environment-specific appsettings file never loaded).
private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{_environment}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
When I then examine the contents of the Configuration, I see that it is only loading values from appsettings.json. It is not loading values from appsettings.Staging.json. The specific value I am looking for is "ConnectionStrings". This is how the ConnectionStrings section looks in appsettings.json:
"ConnectionStrings": {
"ConnectionStringName": "Data Source=SqlDevelopment; Initial Catalog=MyTable; Integrated Security=SSPI;",
}
And this is how that same section looks in appsettings.Staging.json:
"ConnectionStrings": {
"ConnectionStringName": "Data Source=SqlStaging; Initial Catalog=MyTable; Integrated Security=SSPI;",
}
But when I read the DataSource from the Configuration it is always "SqlDevelopment", even though the environment is Staging.
After trying and failing, I tried loading these 4 Nuget packages, but it had no effect:
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.Binder
- Microsoft.Extensions.Configuration.EnvironmentVariables
- Microsoft.Extensions.Configuration.Json
What am I missing?
Host.CreateDefaultBuilder(args).Build();
and gotten the same behavior, along with the logging and DI configurations – Excoriation