A configuration provider loads application settings as key-value pairs from one source. ASP.NET Core can combine several providers into a single configuration object, so settings can change at deployment without recompiling the app. Sources include JSON, XML, and INI files, environment variables, and command-line arguments; other providers can load from services such as Azure Key Vault. section 10.1 section 10.2
Figure 10.2 Using ConfigurationManager to populate IConfiguration. Configuration providers are added to the ConfigurationManager with extension methods. The manager queries the provider and adds all the returned values to the IConfigurationRoot, which implements IConfiguration.
The diagram shows the path from a provider through ConfigurationManager to the IConfiguration values an application reads. section 10.2
WebApplicationBuilder sets up providers automatically, including appsettings.json, an optional environment-specific JSON file, User Secrets for development, environment variables, and command-line arguments. Start with those defaults. If you need another source, add a provider through builder.Configuration; clear builder.Configuration.Sources only when you need to replace the defaults. section 10.2.1 section 10.2.4
Layer settings without replacing every value
In Program.cs, you can clear the defaults, then add sharedSettings.json, appsettings.json, and environment variables in that order. Each provider contributes its keys. If appsettings.json and an environment variable both define MyAppConnString, the environment variable wins because its provider was added last; keys found in only one source remain available. Read the result through IConfiguration. section 10.2.2 section 10.2
Keep secrets out of source control. User Secrets stores local development secrets outside the project folder; environment variables can supply production secrets and override JSON defaults. For a nested setting, an environment variable can use a double underscore, such as MapSettings__MaxNumberOfPoints, where a colon is not allowed. Read it from IConfiguration using the colon-separated key. section 10.2.3
A file provider can reload settings when its file changes: pass reloadOnChange: true to AddJsonFile(). WebApplicationBuilder already enables this for appsettings.json. Reloading is generally a file-provider feature, not an environment-variable-provider feature, and watching many files has a cost. section 10.2.4
Providers can also depend on the hosting environment. An optional appsettings.Development.json, for example, can override shared settings only in development; a User Secrets provider can be included there while a different secret source is used in production. section 10.4.2
