Configuration on.NET Core Configuration

Preface

NET Core has made many changes to the operation of the configuration file relative to the.NET Framework. Let's talk today. Packagage for Configuration starts with Microsoft.Extensions.Configuration and supports a variety of configurations, including memory, Json files, XML files, etc. Today we will mainly use Json format file configurations to demonstrate.

start

Create a new ConsoleApp (which is demonstrated here in a console program instead of ASP.NET Core for demonstration purposes), and add two Package s:

Install-Package Microsoft.Extensions.Configuration -Version 2.0.1
Install-Package Microsoft.Extensions.Configuration.Json -Version 2.0.1

Add Json profile, read configuration

var builder = new ConfigurationBuilder()
	.SetBasePath(Directory.GetCurrentDirectory())
	.AddJsonFile("appsettings.json");

var config = builder.Build();

//Read Configuration
Console.WriteLine(config["Alipay:AppId"]);
Console.WriteLine(config["Alipay:PriviteKey"]);

Our Json file is as follows:

{
  "Alipay": {
    "AppId": "20185555",
    "PriviteKey": "dasfdafafafa"
  } 
}

We create a ConfigurationRoot object from the ConfigurationBuilder object and use it to read the configuration. The SetBasePath() method is used to set the base path of the configuration file needed by our configuration object, for example, if we set the base path to C:\ConsoleApp, then the path where he reads our configuration file appsettings.json will be C:\ConsoleApp\appsettings.json

Function:

What happens when the AddJsonFile method is called multiple times

Let's create a new appsettings.Test.json file and add the following:

{
  "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 that we set up the Json file twice to the ConfigurationBuilder object to see how it works:

You can conclude that the last file added is selected when reading the configuration.

So where did our previous file go? Our ConfigurationRoot object has a Providers property stored, the file information we added, and we can iterate through it:

foreach (var provider in config.Providers)
{
	provider.TryGet("Alipay:AppId", out string val);

	Console.WriteLine(val);
}

Function:

You can see that the values of both of our files have been read!

Configuration overload

Our configuration file may be changed, so how do we get the latest configuration? We can set a parameter called reloadOnChange to true when adding files, then when our files change, the configuration is reloaded into memory, and the configuration we get is up to date. Instead of reading the configuration from the file every time we read it, all the configuration information from the configuration file is loaded into memory, and we read it from memory every time.

var builder = new ConfigurationBuilder()
	.SetBasePath(Directory.GetCurrentDirectory())
	.AddJsonFile("appsettings.json")
	.AddJsonFile("appsettings.Test.json",true,reloadOnChange:true);

var config = builder.Build();

//Read Configuration
Console.WriteLine(config["Alipay:AppId"]);
Console.WriteLine(config["Alipay:PriviteKey"]);

Console.WriteLine("After changing the file, press any key");
Console.ReadKey();

Console.WriteLine("change:");
Console.WriteLine(config["Alipay:AppId"]);
Console.WriteLine(config["Alipay:PriviteKey"]);

Console.ReadKey();

Let's see the effect:

That's it today!

Tags: .NET

Posted on Wed, 01 Dec 2021 14:45:51 -0500 by eb1024