Net AppSettings. Net Core and ASP. Microsoft has replaced ConfigurationManager. AppSettings class of System. Configuration namespace with IConfiguration interface in. Net Core 2. In this article I will explain with an example, how to use ConfigurationManager.
What is IConfiguration. The IConfiguration is an interface for. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. You will need to import the following namespace. In order to add AppSettings. Once the File is created, it will have a DefaultConnection , below that a new AppSettings entry is added. Reading AppSettings from AppSettings. In the below example, the IConfiguration is injected in the Controller and assigned to the private property Configuration.
GetSection "AppSettings" [ "Site" ];. In this series I'm going to be looking at some of the code behind some of those features. In this first post, I take a look at the ConfigurationManager class, why it was added, and some of the code used to implement it.
If your first response is "what's ConfigurationManager ", then don't worry, you haven't missed a big announcement!
ConfigurationManager was added to support ASP. NET Core startup code. However ConfigurationManager is very much an implementation detail. It was introduced to optimise a specific scenario which I'll describe shortly , but for the most part, you don't need to and won't know you're using it. Before we get to the ConfigurationManager itself, we'll look at what it's replacing and why. NET 5 exposes multiple types around configuration, but the two primary ones you use directly in your apps are:.
The IConfigurationBuilder interface is mostly a wrapper around a list of configuration sources. The IConfigurationRoot meanwhile represents the final "layered" configuration values, combining all the values from each of the configuration sources to give a final "flat" view of all the configuration values. If you were using the types directly, you might do something like this:. In a typical ASP. NET Core app you wouldn't be creating the ConfigurationBuilder yourself, or calling Build , but otherwise this is what's happening behind the scenes.
There's a clear separation between the two types, and for the most part, the configuration system works well, so why do we need a new type in. NET 6? The main problem with this design is when you need to "partially" build configuration. This is a common problem when you store your configuration in a service such as Azure Key Vault, or even in a database.
NET Core:. Configuring the Azure Key Vault provider requires a configuration value, so you're stuck with a chicken and egg problem—you can't add the configuration source until you have built the configuration! This whole dance is a little messy, but there's nothing wrong with it per-se, so what's the downside? The downside is that we have to call Build twice: once to build the IConfigurationRoot using only the first sources, and then again to build the IConfiguartionRoot using all the sources, including the Azure Key Vault source.
In the default ConfigurationBuilder implementation, calling Build iterates over all of the sources, loading the providers, and passing these to a new instance of the ConfigurationRoot :. The ConfigurationRoot then loops through each of these providers in turn and loads the configuration values. Generally speaking, there's no harm in fetching the data from a configuration source more than once, but it's unnecessary work, and often involves relatively slow reading of files etc.
This is such a common pattern, that in. NET 6 a new type was introduced to avoid this "re-building", ConfigurationManager. As part of the "simplified" application model in. NET 6, the. NET team added a new configuration type, ConfigurationManager. By combining both implementations in a single type,.
NET 6 can optimise the common pattern show in the previous section. With ConfigurationManager , when an IConfigurationSource is added when you call AddJsonFile for example , the provider is immediately loaded, and the configuration is updated. This can avoid having to load the configuration sources more than once in the partial-build scenario.
This contains a reference to the ConfigurationManager instance, so that any changes can be reflected in the configuration:. For more information, see Configuring Apps by using Configuration Files. You can use the built-in System. Configuration types or derive from them to handle configuration information. By using these types, you can work directly with configuration information and you can extend configuration files to include custom information.
The ConfigurationManager class includes members that enable you to perform the following tasks:. Read a section from a configuration file. To access configuration information, call the GetSection method. These members perform read-only operations, use a single cached instance of the configuration, and are multithread aware.
Read and write configuration files as a whole. Your application can read and write configuration settings at any level, for itself or for other applications or computers, locally or remotely.
Use one of the methods provided by the ConfigurationManager class to open a configuration file such as SampleApp. These methods return a Configuration object that in turn exposes methods and properties you can use to work with the associated configuration files. The methods perform read or write operations and create the configuration data every time that a file is written.
Support configuration tasks. The following types are used to support various configuration tasks:. In addition to working with existing configuration information, you can create and work with custom configuration elements by extending the built-in configuration types such as the ConfigurationElement , ConfigurationElementCollection , ConfigurationProperty , and ConfigurationSection classes.
For an example of how to extend a built-in configuration type programmatically, see ConfigurationSection. For an example of how to extend a built-in configuration type that uses the attribute-based model, see ConfigurationElement.
The Configuration class enables programmatic access for editing configuration files. You use one of the Open methods provided by ConfigurationManager. These methods return a Configuration object, which in turn provides the required methods and properties to handle the underlying configuration files.
0コメント