Reputation: 139
We need to read from multiple appsettings based on local or not. We achieved this by adding appsettings.local.json
.
But we see that although we add this file and set up necessary configurations, we were not able to read config data from appsettings.local.json
, all time it saw appsettings.json
.
Let me tell by code:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.local.json", optional: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
CreateWebHostBuilder(args, config).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args, IConfiguration config)
{
var webHostBuilder = WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.ConfigureServices(services => services.AddSingleton<IConfiguration>(config))
.UseStartup<Startup>();
return webHostBuilder;
}
In CreateWebHostBuilder
, if we do not add .ConfigureServices(services => services.AddSingleton<IConfiguration>(config))
we could not read data from appsettings.local.json
. After adding this, we can read from appsettings.local.json
. So my question is what happened in here? Can someone provide a reasonable explanation?
Upvotes: 2
Views: 3702
Reputation: 388023
What you are doing there is that you create a separate configuration builder that loads its own sources. You do this although the default web host builder already contains a few configuration sources, including the appsettings.json
and appsettings.<environment>.json
files.
By saying services.AddSingleton<IConfiguration>(config)
you are effectively replacing the default configuration with your own configuration. While this will probably have the right effect most of the time, I can imagine that it can lead to some problems later since you effectively have two different configurations within your application (although one of them—the default one—is mostly inaccessible).
I would generally recommend you not to do this but instead to change the default configuration so that it loads all your desired sources. You can do that by calling WebHostBuilder.ConfigureAppConfiguration
:
public static IWebHostBuilder CreateWebHostBuilder(string[] args, IConfiguration config)
{
var webHostBuilder = WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("appsettings.local.json", optional: true);
})
.UseStartup<Startup>();
return webHostBuilder;
}
This will now add another JSON file to the default configuration builder. So the default IConfiguration
will take the configuration values from that source as well. This way, you won’t be needing to create the ConfigurationBuilder
within your Program.cs
.
Note that the appsettings.local.json
is now the last configuration source, so whatever settings are configured there will overwrite previously configured settings. So be aware of this if you are using command line arguments or environment variables.
Finally note that if you meant to use the local
file only on a specific local environment, then you can also just set the hosting environment to be local
to load the appsettings.local.json
automatically as part of the default configuration setup. This is because the default host builder will automatically pick up appsettings.<environment>.json
if it exists.
Upvotes: 3