Reputation: 1077
Upgraded a console app to .NET 6 and found that .NET 6 automatically reads in appsettings.json.
Previous code looked like this in .NET 6
var host = Host.CreateDefaultBuilder(args).
ConfigureHostConfiguration(hostingContext =>
{
var env = Environment.GetEnvironmentVariable("APP_HOST_ENV");
hostingContext.SetBasePath(AppContext.BaseDirectory);
if (env == "Development")
{
hostingContext.AddJsonFile($"appsettings.{env}.json", true, true);
}
else
{
hostingContext.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
}
});
Each json file was only loaded based on the env. Now it seems that .NET 6 Host includes the appsettings.json by default.
Don't want appsettings.json to be loaded by default.
What are my options?
Upvotes: 3
Views: 2372
Reputation: 11173
True, .CreateDefaultBuilder
calls .ConfigureDefaults
which is documented here.
I believe you can create your new HostBuilder()
directly, then choose which other defaults you need to implement. But it looks like there are a large number of defaults that would be complicated to re-implement.
Instead, you may be able to remove the FileConfigurationSource
sources;
.ConfigureAppConfiguration((ctx, config) =>
{
config.Sources.Clear();
...
Upvotes: 0
Reputation: 101
The appsettings.json is chosen depending on the "DOTNET_ENVIRONMENT" and "ASPNETCORE_ENVIRONMENT" environment variables. The latter overrides the former.
"Production" is the default value if "DOTNET_ENVIRONMENT" and "ASPNETCORE_ENVIRONMENT" have not been set.
Check out the Microsoft doc on using multiple environment. It's pretty detailed.
Here is how to set it in code, since I'm guessing that's what you want to do.
Upvotes: 1