Reputation:
I am new in .NET Core and as far as I see from my search on the web, appsettings.Development.json
is used for development config while developing the app and appsettings.Production.json
is used on the published app in production server. Here are the example part that I set on both:
appsettings.Development.json:
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,21433;Initial Catalog=DemoDbDev;User Id=demouser;Password=******;"
},
appsettings.Production.json:
"ConnectionStrings": {
"DefaultConnection": "Server=demo-prod-db,1433;Initial Catalog=DemoDbProd;User Id=demouser;Password=******;"
},
Here are some question that I need to be clarified:
1) What is the purpose of the appsettings.json
? Is it used for default config or for the other settings that are not in the appsettings.Development.json
or appsettings.Production.json
?
2) Is the environment set on the launchSettings.json
in the Properties
folder? If so, should we manually update this file before publishing the app to the production server?
3) Is there anything that I should keep in mind regarding to Development and Production environment configs while developing and publishing my app (to IIS Server or Docker container)?
Any help would be really appreciated.
Upvotes: 42
Views: 38798
Reputation: 26362
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-5.0
TLDR; You can get the environment from an environment variable or the launch settings file. The latter is only used for local development.
To determine the runtime environment, ASP.NET Core reads from the following environment variables:
DOTNET_ENVIRONMENT ASPNETCORE_ENVIRONMENT when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call ConfigureWebHostDefaults. The ASPNETCORE_ENVIRONMENT value overrides DOTNET_ENVIRONMENT. IHostEnvironment.EnvironmentName can be set to any value, but the following values are provided by the framework:
Development : The launchSettings.json file sets ASPNETCORE_ENVIRONMENT to Development on the local machine
Staging
Production : The default if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT have not been set.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0
TLDR; You set the defaults and non changing values in the plain json and the overrides in the environment json, as you described.
The default JsonConfigurationProvider loads configuration in the following order:
- appsettings.json
- appsettings.Environment.json : For example, the appsettings.Production.json and appsettings.Development.json files.
The environment version of the file is loaded based on the IHostingEnvironment.EnvironmentName.
appsettings.Environment.json values override keys in appsettings.json.
Secrets and passwords should not be stored for production environments in the configuration files.
Consider using services like key vault on azure or database encrypted configurations or build server variables that will override the environment specific secrets.
Upvotes: 29