When using the IConfigurationBuilder in a .NET Core 2.1 application with a Generic Host I configure 4 sources; but after the scope of ConfigureAppConfiguration there are 6 sources.
At some point 2 additional source I have already loaded are added a second time in an order that is causing appsettings.Environment.json values to be hidden. I have also tried removing the hostsettings.json configuration and verified that is not affecting this. This is for an Azure Webjob using WebjobsSDK 3.0 and .Net Core 2.1
var builder = new HostBuilder()
.ConfigureHostConfiguration(configurationBuilder =>
{
//This is to do some basic host configuration and should only add 2 sources
configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
configurationBuilder.AddJsonFile("hostsettings.json", optional: true);
configurationBuilder.AddEnvironmentVariables(prefix: "APPSETTING_ASPNETCORE_");
})
.ConfigureAppConfiguration((hostContext, configurationBuilder) =>
{
//at this point there are 0 sources in the sources
IHostingEnvironment env = hostContext.HostingEnvironment;
configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
configurationBuilder.AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appSettings.{env.EnvironmentName}.json", optional: true,
reloadOnChange: true);
configurationBuilder.AddEnvironmentVariables(prefix: "APPSETTING_ASPNETCORE_");
//at this point there are 4 sources
})
.ConfigureServices((hostContext, servicesCollection) =>
{
//now there are 6, 2 additional source that are duplicates
servicesCollection.Configure<IConfiguration>(hostContext.Configuration);
})
I expect a configuration provider with only the 4 sources, including the ChainedConfigSource, I have setup to be included. But 2 additional sources are added which are duplicates of the appsettings.json and the environment variables which I declared before loading the environment specific appsettings.environment.json.
Now when injected the into a class the appsettings.json settings were added last are returned over a appsettings.environment.json
But 2 additional sources are added which are duplicates of the
appsettings.json
and the environment variables
I had a similar issue with an Azure WebJob using the HostBuilder
, and noticed that these 2 source were appended to the end of the list of config sources. This had undesirable results: development settings from appsettings.json
overwrote the production settings fromappsettings.Production.json
.
These additional sources appear to be added here by by ConfigureWebJobs
.
The fix was to re-order the HostBuilder
call chain so that the call to ConfigureWebJobs
comes before the call to ConfigureAppConfiguration
. These extra two sources are still present, but since they are now at the start of the list of configuration sources, and not at the end, they have no undesirable effects.