.NET CORE configuration options — configuration

Configuration options represent two separate base frameworks:

Configuration model

The configuration of .NET Core is presented in the form of IConfiguration object in the process of being consumed by the application, so it is called configuration tree, and the name of the configuration tree reflects the configuration Logical structure.

The raw data for the configuration is varied. The ultimate purpose of the configuration model is to extract raw configuration data and convert it into an IConfiguration object.

How is the configuration converted from the original structure to the logical structure?

Remark:

After the original configuration data is read, it will be converted into a configuration dictionary. The reason why it can be converted into a configuration dictionary is because the structure of the configuration tree and the data it carries can be expressed through a dictionary. We only need to use the path of the configuration tree as the key , just use the carried data as value. After converting to a configuration dictionary, complete the conversion for logic.

IConfiguration

Why is the configuration tree a logical structure?

The fact that an IConfiguration object has a tree hierarchy does not mean that the corresponding type has a corresponding data member definition, but that the API it provides logically reflects the tree hierarchy, so the configuration tree is a logical structure.

The IConfiguration object where the root node is located is represented by an IConfigurationRoot object, and other node objects are represented by an IConfigurationSection object

key: Uniquely identifies multiple ConfigurationSection objects with the same parent node

path: Indicates the path of the current configuration node in the configuration tree, consisting of the Keys of all IConfigurationSection objects that make up the current path, and colons (:) are used as separators between Keys

value: leaf nodes have data, non-leaf nodes return null

Reload: Reload the configuration data. The IConfigurationRoot object represents the root of the configuration tree, so it also represents the entire configuration tree. If it is reloaded, it means that all the configuration data carried by the entire configuration tree are reloaded

The ConfigurationRoot object keeps references to the IConfiguration Provider objects provided by all registered IConfigurationSource objects. When calling the corresponding API of the ConfigurationRoot object or the ConfigurationSection object to extract configuration data, the data will eventually be directly extracted from these IConfigurationProvider objects . In other words, configuration data is only stored on the IConfigurationProvider object in the form of configuration dictionary in the whole model.

IConfigurationProvider

Although each different type of configuration source has a corresponding IConfigurationSource implementation, the reading of raw data is not done by it, but by a corresponding IConfigurationProvider object.

The purpose of the IConfigurationProvider object is to convert the configuration data from the original structure into a configuration dictionary, so the methods defined in the IConfigurationProvider interface are mostly embodied as operations on dictionary objects

load: load configuration data

set: The set configuration data is generally only stored in memory, and is not responsible for persisting the updated configuration data

Each type of configuration source corresponds to an implementation type of the IConfigurationProvider interface, but they generally do not directly implement the IConfigurationProvider interface, but choose to inherit another abstract class named ConfigurationProvider

IConfigurationSource

The IConfigurationSource object represents the configuration source in the configuration model and is registered to the IConfigurationBuilder object to provide the original configuration data for the IConfiguration object created by it . Since the reading of the original configuration data is implemented in the corresponding IConfigurationProvider object, the function of the IConfigurationSource object is to provide the corresponding IConfigurationProvider object

The IConfigurationBuilder object uses the IConfigurationProvider object provided by all IConfigurationSource objects registered on it to read the original configuration data and create the corresponding IConfiguration object.

IConfigurationBuilder

The IConfigurationBuilder object will use the raw data provided by the registered IConfigurationSource object to create an IConfiguration object for use by the application.

The configuration system provides a class named ConfigurationBuilder as the default implementation of the IConfigurationBuilder interface. The Build method of type ConfigurationBuilder returns an object of type ConfigurationRoot, and each non-root configuration node of the configuration tree represented by it is an object of type ConfigurationSection.

One of the main features of the configuration model is to provide support for multiple configuration sources

The configuration source will be registered on the IConfigurationBuilder object, which provides the corresponding IConfigurationProvider object, which is responsible for reading the configuration source and converting the configuration source into a configuration dictionary

MemoryConfigurationSource

InitialData: store initial configuration data

The role of the IConfigurationProvider object in the configuration model is to read the original configuration data and convert it into a configuration dictionary.

The Add method defined by MemoryConfigurationProvider can add a new configuration item to the configuration dictionary at any time.

EnvironmentVariablesConfigurationSource

The extraction and maintenance of environment variables can be done through the static type Environment

CommandLineConfigurationSource

CommandLineConfigurationSource type is defined in NuGet package ‘Microsoft.Extensions.Configuration.CommandLine’

The fundamental purpose of CommandLineConfigurationSource is to convert named line switches from an array of strings to a dictionary of configuration

FileConfigurationSource

Physical files are the most commonly used original configuration carriers. There are three main configuration file formats: json, xml, and ini. The corresponding configuration source types are JsonConfigurationSource, XmlConfigurationSource, and IniConfigurationSource. They have the same base class as follows: FileConfigurationSource

IFileProvider object

To read the configuration file, you can use the FileProvider property to set this object

path

The path of the configuration file can be represented by the Path attribute, which is generally a relative path to the root directory of the IFileProvider object

When reading the configuration file, this path will be used as a parameter to call the GetFileInfo method of the IFileProvider object to obtain the IFileInfo object describing the configuration file, and the CreateReadStream method of the object will be used in the end Call to read the file content

ResolveFileProvider

If the FileProvider property is not explicitly assigned, and the specified configuration file path is an absolute path (such as “c:\app\appsettings.json”), then a directory for the configuration file (“c:\ \app”), as the property value of FileProvider, and the Path property will be set to the configuration file name.

EnsureDefaults

This method ensures that the FileConfigurationSource always has an IFileProvider object for loading configuration files. Specifically, the EnsureDefaults method will eventually call the IConfigurationBuilder interface with the GetFileProvider extension method defined as follows to obtain the default IFileProvider object

//The GetFileProvider extension method is actually a dictionary representing the Properties property of the IConfiguration Builder object
//As a container for storing IFileProvider objects (the corresponding Key is FileProvider).
//If there is an IFileProvider object in this container, it will be used as the return value of the method. Instead, the method will apply the currently applied
//The base directory (the default is the base directory of the current application domain, that is, the directory where the currently executing .exe file is located) as the root directory to create a
//PhysicalFileProvider object
public static class FileConfigurationExtensions
{
    public static IFileProvider GetFileProvider(this IConfigurationBuilder builder)
    {
        if (builder. Properties. TryGetValue("FileProvider", out object provider))
        {
            return builder.Properties["FileProvider"] as IFileProvider;
        }
        return new PhysicalFileProvider(AppContext. BaseDirectory  string. Empty);
    }
}

Optional

Indicates whether the current configuration source can be defaulted. If this property is set to False, no exception will be thrown even if the specified configuration file does not exist

ReloadOnChange

Whether to monitor the file system generally requires the use of the ReloadDelay attribute. The unit of this attribute is milliseconds, and the default delay is 250 milliseconds.

StreamConfigurationSource

The StreamConfigurationSource object reads configuration content through the specified Stream object, so this configuration source has more flexible applications

ChainedConfigurationSource

Custom ConfigurationSource

Configuration binding

Configuration binding: the process of converting the extracted configuration data into POCO objects and using the configuration in an object-oriented manner

Configuration binding can be implemented through the following extension methods for IConfiguration, which are defined in the NuGet package “Microsoft.Extensions.Configuration.Binder”.

The so-called configuration binding is reflected in how to convert an IConfiguration object (which can be an IConfigurationRoot object or an IConfigurationSection object) mapped to a node on the configuration tree into a corresponding POCO object.

Configuration Synchronization

Synchronization of configuration involves two aspects:

First, monitor the original configuration source and reload the configuration after it changes;

Second, notify the application in time after the configuration is reloaded, so that the application can use the latest configuration in time

The IConfiguration Builder object at the core, with the help of the IConfigurationProvider object provided by the registered IConfigurationSource object, loads data from the corresponding configuration source, and various implementations of the IConfigurationProvider interface are to convert the original configuration data in various forms into a configuration dictionary .

ConfigurationReloadToken

For the IChangeToken object returned by the GetReloadToken method of the IConfiguration interface, its function is not to send a notification to the application when the configuration source changes, but to notify the application that the configuration source has changed and the new data has been Reloaded by the corresponding IConfigurationProvider object

Since the Configuration Root object and the ConfigurationSection object do not maintain any data, they only transfer the API call to the IConfigurationProvider object, so the application can obtain the latest configuration data using the original IConfiguration object.

ConfigurationRoot

A ConfigurationRoot object is created from a set of IConfigurationProvider objects provided by registered IConfigurationSource objects.

GetReloadToken:

What is returned is a ConfigurationReloadToken object, which is represented by the field _changeToken. If you need to use this object to send a configuration reload notification, you need to call its OnReload method. Since an IChangeToken object can only send a notification once, this method is also responsible for creating a new ConfigurationReload Token object and assigning a value to the _changeToken field.

In other words, once the RaiseChanged method of ConfigurationRoot is called, we can use the IChangeToken object returned by its GetReloadToken method to receive notifications that the configuration has been reloaded.

If the IConfiguration Provider objects provided by multiple IConfigurationSource configuration sources contain configuration items with the same name, the IConfigurationSource objects registered later will have a higher selection priority, and we should reasonably arrange the registration order of the IConfigurationSource objects according to this feature