Objective
. NET Core has made a lot of changes to the. NET Framework with respect to the operation of the configuration file, so let's talk today. The package for the configuration is the Microsoft.Extensions.Configuration
beginning of a variety of support configurations, including memory, JSON files, XML files, and so on, today we mainly use the JSON format file configuration to demonstrate.
Begin
Create a new ConsoleApp (demo with a console program for demonstration purposes instead of ASP. NET Core) to add two packages:
Install-Package Microsoft.Extensions.Configuration -Version 2.0.1Install-Package Microsoft.Extensions.Configuration.Json -Version 2.0.1
Add a JSON configuration file, read the configuration
var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json");var config = builder.Build();//读取配置Console.WriteLine(config["Alipay:AppId"]);Console.WriteLine(config["Alipay:PriviteKey"]);
The contents of our JSON file are as follows:
{ "Alipay":{ "AppId":"20185555", "PriviteKey":"dasfdafafafa" }}
We use ConfigurationBuilder
objects to create ConfigurationRoot
objects and use them to read the configuration. SetBasePath()
method is used to set the base path of the configuration file required by our configuration object, for example, we set the base path to C:\ConsoleApp
, then he reads our configuration file appsettings.json
path will beC:\ConsoleApp\appsettings.json
Run:
What happens when you call the Addjsonfile method multiple times
We create a new appsettings.Test.json
file and add the following content:
{ "Alipay":{ "AppId":"20185555Testss", "PriviteKey":"dasfdafafafaTestss" }}
Then modify the code:
var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddJsonFile("appsettings.Test.json");
You can see ConfigurationBuilder
that we set the JSON file two times to the object to see how it works:
It can be concluded that the last file added is selected when the configuration is read.
So where did we go from the previous file? Our ConfigurationRoot
object has a Providers
property stored, we add the file information, we can traverse it:
foreach (var provider in config.Providers){ provider.TryGet("Alipay:AppId", out string val); Console.WriteLine(val);}
Run:
You can see that the values of our two files have been read!
Configure overloading
Our configuration files may be changed, so how do we get the latest configuration? When we add a file we can set a parameter named reloadOnChange
true, then when our file changes, it will reload the configuration into memory, and then we get to the configuration is the latest. When we read the configuration, not every read is read from the file, but all configuration information of the configuration file is loaded into memory, and every time we read it from memory.
var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddJsonFile("appsettings.Test.json",true,reloadOnChange:true);var config = builder.Build();//读取配置Console.WriteLine(config["Alipay:AppId"]);Console.WriteLine(config["Alipay:PriviteKey"]);Console.WriteLine("更改文件之后,按下任意键");Console.ReadKey();Console.WriteLine("change:");Console.WriteLine(config["Alipay:AppId"]);Console.WriteLine(config["Alipay:PriviteKey"]);Console.ReadKey();
Let's look at the effect:
It's here today!
. NET Core Configuration