liveBook · Manning

Configuration provider

As explained in ASP.NET Core in Action, 3e

A configuration provider is a component that reads application configuration values from a particular source and contributes them to the configuration system.

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.
CH10_F02_Lock3

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains provider registration, defaults, override order, secret sources, and file reloading with Program.cs examples.
Examples
Uses layered JSON files and environment variables to show how a later provider overrides a connection-string setting.
What it assumes
Assumes a WebApplicationBuilder-based ASP.NET Core app while defining providers and showing how to register one.
What you can do after
Operate/configure ASP.NET Core providers to add sources, control overrides, and keep development and production secrets out of source control.
How it connects
Connects ConfigurationManager and IConfiguration with hosting environments, User Secrets, and configuration reloading.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.