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
So according to the Documentation the WebHostBuilder loads the appSettings.json and appSettings.env.json files for you. But it does not say anything about the HostBuilder doing this as well, I believe this is due to a lack of documentation and I cannot determine where in the source code that is coming from.
To resolve this issue, I changed the way my configuration files are setup. Previously I had connection strings in the appSettings.json file as well as the appSettings.env.json files. So I was having an issue with the configuration files added last replacing the values from the base configuration file. I have now moved environment based settings only into the configuration files that are for each environment and only kept the settings that were global to all environments in the base configuration file.
Seems like old habits from the .NET framework configuration transformation set ups die hard. I cannot determine if the same key in a IConfiguration declared from multiple providers should be changed to the last loaded provider, I thought some documentation covered this and confirmed this but now I cannot find it.