The new configuration system uses .NET Core [8]: how to synchronize configuration and source files

Configuration Synchronization involves two aspects: First, the original profile and the implementation of monitoring its newly loaded configuration after change; notify after the second application, and further arranged so that the latter can be re-loaded with the latest configuration. Next, we use a simple .NET Core console application to demonstrate involved will file for configuration data synchronization problem, we want to be able to monitor the implementation of the original application configuration file, and the new application is loaded and when the file content changes the new configuration. JSON configuration file for the source is represented by JsonConfigurationSource type, which is defined in the "Microsoft.Extensions.Configuration.Json" NuGet this package, so we need to add a dependency for the NuGet package form according to the following file project.json . [This article has been synchronized to the " ASP.NET Framework Core Secret " among]

   1: {
   2:   ...
   3:   "dependencies": {
   4:     ...    
   5:     "Microsoft.Extensions.Configuration.Json": "1.0.0"
   6:   }
   7: }

Suppose we need to configure the application to use the current capacity of the thread pool, so the settings need to be adjusted according to the current load, it requires high timeliness, we want to modify the configuration once JSON file an application for a thread pool the settings can take effect immediately. For simplicity, we only define MinThreads and both are determined MaxThreads CI thread pool size range, ThreadPoolOptions is shown below the corresponding type Options.

   1: public class ThreadPoolOptions
   2: {
   3:     public int MinThreads { get; set; }
   4:     public int MaxThreads { get; set; }
   5:  
   6:     public override string ToString()
   7:     {
   8:         return $"Thread pool size: [{MinThreads}, {MaxThreads}]";
   9:     }
  10: }

We add a file named threadPool.json in the project to define the configuration of the thread pool. In addition, we need to make the file by modifying project.json and compile the configuration is automatically copied to the output directory (by default bin directory) at compile time. Specifically, we just need to set the path to the file configuration values ​​for optional "builtOptions / copyToOutput" on it in the following manner.

   1: {
   2:   ...
   3:   "buildOptions": {
   4:     ...
   5:     "copyToOutput": "threadPool.json"
   6:   }
   7: }

How then we wrote a program to demonstrate the application of the following configurations used in sync with the contents of the configuration file. We first created a ConfigurationBuilder objects, and on it registered a JsonConfigurationSource. In creating this JsonConfigurationSource object, apart from the specified configuration file ( "threadPool.json") path, we will its ReloadOnChange property is set to True. As the name suggests, this time meaning ReloadOnChange property is when the contents of the original configuration file is changed whether it is necessary to re-load the configuration.

   1: IConfiguration config = new ConfigurationBuilder()
   2:     .Add(new JsonConfigurationSource {Path = "threadPool.json", ReloadOnChange = true })
   3:     .Build();
   4:  
   5: Action changeCallBack = () => {
   6:     ThreadPoolOptions options = new ServiceCollection()
   7:         .AddOptions()
   8:         .Configure<ThreadPoolOptions>(config)
   9:         .BuildServiceProvider()
  10:         .GetService<IOptions<ThreadPoolOptions>>()
  11:         .Value;
  12:     Console.WriteLine(options);
  13: };
  14:  
  15: ChangeToken.OnChange(()=>config.GetReloadToken(), changeCallBack);
  16:  
  17: Random random = new Random();
  18: while (true)
  19: {
  20:     ThreadPoolOptions options = new ThreadPoolOptions
  21:     {
  22:         MinThreads = random.Next(10, 20),
  23:         MaxThreads = random.Next(40, 50)
  24:     };
  25:     File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "threadPool.json"), JsonConvert.SerializeObject(options));
  26:     Task.Delay(5000).Wait();
  27: }

After obtaining the Configuration object using the ConfigurationBuilder, we call it GetReloadToken method to get a ChangeToken object, which will help us determine whether the configuration is reloaded. We call ChangeToken type of static methods OnChange registered a callback for this ChangeToken the object, the callback will be automatically executed when the configuration is reloaded. As for the registered callback, we just adopt Options model has been ThreadPoolOptions object configuration binding is generated, and its associated information is printed on the console.

At the end of this program, we are in an infinite loop at 5-second intervals updates threadPool.json file. In accordance with the intent of this procedure, when every time we completed the update for the threadPool.json, Configuration objects we create will be automatically reloaded. Configuration Once reloaded, call HasChanged attribute its GetReloadToken method to get ChangeToken object before will become True, it is registered in the above callback will be executed. So the end result is to re-configure the settings will immediately appear on the console, the output shown below confirms this. (S04)

image

 

Configuration Synchronization involves two aspects: First, the original profile and the implementation of monitoring its newly loaded configuration after change; notify after the second application, and further arranged so that the latter can be re-loaded with the latest configuration. Next, we use a simple .NET Core console application to demonstrate involved will file for configuration data synchronization problem, we want to be able to monitor the implementation of the original application configuration file, and the new application is loaded and when the file content changes the new configuration. JSON configuration file for the source is represented by JsonConfigurationSource type, which is defined in the "Microsoft.Extensions.Configuration.Json" NuGet this package, so we need to add a dependency for the NuGet package form according to the following file project.json . [This article has been synchronized to the " ASP.NET Framework Core Secret " among]

   1: {
   2:   ...
   3:   "dependencies": {
   4:     ...    
   5:     "Microsoft.Extensions.Configuration.Json": "1.0.0"
   6:   }
   7: }

Suppose we need to configure the application to use the current capacity of the thread pool, so the settings need to be adjusted according to the current load, it requires high timeliness, we want to modify the configuration once JSON file an application for a thread pool the settings can take effect immediately. For simplicity, we only define MinThreads and both are determined MaxThreads CI thread pool size range, ThreadPoolOptions is shown below the corresponding type Options.

   1: public class ThreadPoolOptions
   2: {
   3:     public int MinThreads { get; set; }
   4:     public int MaxThreads { get; set; }
   5:  
   6:     public override string ToString()
   7:     {
   8:         return $"Thread pool size: [{MinThreads}, {MaxThreads}]";
   9:     }
  10: }

We add a file named threadPool.json in the project to define the configuration of the thread pool. In addition, we need to make the file by modifying project.json and compile the configuration is automatically copied to the output directory (by default bin directory) at compile time. Specifically, we just need to set the path to the file configuration values ​​for optional "builtOptions / copyToOutput" on it in the following manner.

   1: {
   2:   ...
   3:   "buildOptions": {
   4:     ...
   5:     "copyToOutput": "threadPool.json"
   6:   }
   7: }

How then we wrote a program to demonstrate the application of the following configurations used in sync with the contents of the configuration file. We first created a ConfigurationBuilder objects, and on it registered a JsonConfigurationSource. In creating this JsonConfigurationSource object, apart from the specified configuration file ( "threadPool.json") path, we will its ReloadOnChange property is set to True. As the name suggests, this time meaning ReloadOnChange property is when the contents of the original configuration file is changed whether it is necessary to re-load the configuration.

   1: IConfiguration config = new ConfigurationBuilder()
   2:     .Add(new JsonConfigurationSource {Path = "threadPool.json", ReloadOnChange = true })
   3:     .Build();
   4:  
   5: Action changeCallBack = () => {
   6:     ThreadPoolOptions options = new ServiceCollection()
   7:         .AddOptions()
   8:         .Configure<ThreadPoolOptions>(config)
   9:         .BuildServiceProvider()
  10:         .GetService<IOptions<ThreadPoolOptions>>()
  11:         .Value;
  12:     Console.WriteLine(options);
  13: };
  14:  
  15: ChangeToken.OnChange(()=>config.GetReloadToken(), changeCallBack);
  16:  
  17: Random random = new Random();
  18: while (true)
  19: {
  20:     ThreadPoolOptions options = new ThreadPoolOptions
  21:     {
  22:         MinThreads = random.Next(10, 20),
  23:         MaxThreads = random.Next(40, 50)
  24:     };
  25:     File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "threadPool.json"), JsonConvert.SerializeObject(options));
  26:     Task.Delay(5000).Wait();
  27: }

After obtaining the Configuration object using the ConfigurationBuilder, we call it GetReloadToken method to get a ChangeToken object, which will help us determine whether the configuration is reloaded. We call ChangeToken type of static methods OnChange registered a callback for this ChangeToken the object, the callback will be automatically executed when the configuration is reloaded. As for the registered callback, we just adopt Options model has been ThreadPoolOptions object configuration binding is generated, and its associated information is printed on the console.

At the end of this program, we are in an infinite loop at 5-second intervals updates threadPool.json file. In accordance with the intent of this procedure, when every time we completed the update for the threadPool.json, Configuration objects we create will be automatically reloaded. Configuration Once reloaded, call HasChanged attribute its GetReloadToken method to get ChangeToken object before will become True, it is registered in the above callback will be executed. So the end result is to re-configure the settings will immediately appear on the console, the output shown below confirms this. (S04)

image

 

Guess you like

Origin www.cnblogs.com/webenh/p/11589968.html