(83) C# .net core 配置系统
配置文件有多种读取方式
先引用,再根据不同的源添加不同的读取方式
Microsoft.Extensions.Configuration
一、读取json文件
再添加
Microsoft.Extensions.Configuration.Json
创建一个config.json文件,文件名可以随意.
{ "name": "tom", "age": "18", "proxy": {"address":"china"} }
文件属性设置成,如果比较新则复制
1.读取配置文件程序
using Microsoft.Extensions.Configuration; ConfigurationBuilder configBuilder = new ConfigurationBuilder(); //第一个参数要读取文件名 //optional 没有该文件则不报错,默认false。 //reloadOnChange 修改文件后,是否立即读取默认false。无需重启程序 configBuilder.AddJsonFile("config.json", optional: false, reloadOnChange: true); IConfigurationRoot config = configBuilder.Build(); string name = config["name"]; Console.WriteLine(name);
测试不重启的情况下,修改配置文件
ConfigurationBuilder configBuilder = new ConfigurationBuilder(); //第一个参数要读取文件名 //optional 没有该文件则不报错,默认false。 //reloadOnChange 修改文件后,是否立即读取默认false。无需重启程序 configBuilder.AddJsonFile("config.json", optional: false, reloadOnChange: true); IConfigurationRoot config = configBuilder.Build(); while (true) { Proxy proxy = config.GetSection("proxy").Get<Proxy>(); Console.WriteLine(proxy.Address); Thread.Sleep(3000); }
即时修改
2.读取多节点
string name = config.GetSection("proxy:address").Value;
3.绑定成类
Microsoft.Extensions.Configuration.Binder
程序
using Microsoft.Extensions.Configuration; ConfigurationBuilder configBuilder = new ConfigurationBuilder(); //第一个参数要读取文件名 //optional 没有该文件则不报错,默认false。 //reloadOnChange 修改文件后,是否立即读取默认false。无需重启程序 configBuilder.AddJsonFile("config.json", optional: false, reloadOnChange: true); IConfigurationRoot config = configBuilder.Build(); Proxy proxy = config.GetSection("proxy").Get<Proxy>(); Console.WriteLine(proxy); public class Proxy { public string Address { get; set; } }
3.结合依赖注入
需要引入
Microsoft.Extensions.Configuration.Binder
Microsoft.Extensions.Options
三种方式
3.1 不会读配置文件的新值
IOptions<T>
3.2 立即读配置文件的新值
IOptionsMonitor<T>
Program.cs
using ConsoleTest; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; ConfigurationBuilder configBuilder = new ConfigurationBuilder(); configBuilder.AddJsonFile("config.json", optional: true, reloadOnChange: true); IConfigurationRoot configRoot = configBuilder.Build(); ServiceCollection services = new ServiceCollection(); services.AddTransient<Test>(); //把Proxy绑定到根节点 services.AddOptions().Configure<Proxy>(e => configRoot.GetSection("proxy").Bind(e)); while (true) { var serviceProvider = services.BuildServiceProvider(); var test = serviceProvider.GetService<Test>(); test.ok(); Thread.Sleep(3000); } Console.ReadKey(); public class Proxy { public string Address { get; set; } }
Test.cs
using Microsoft.Extensions.Options; namespace ConsoleTest { public class Test { //即时读取 private readonly IOptionsMonitor<Proxy> optionsMonitor; public Test(IOptionsMonitor<Proxy> optionsMonitor) { this.optionsMonitor = optionsMonitor; } public void ok() { var config = optionsMonitor.CurrentValue; Console.WriteLine(config.Address); } } }
可以同时绑定多个对象
ConfigurationBuilder configBuilder = new ConfigurationBuilder(); configBuilder.AddJsonFile("config.json", optional: true, reloadOnChange: true); IConfigurationRoot configRoot = configBuilder.Build(); ServiceCollection services = new ServiceCollection(); services.AddTransient<Test>(); //同时注入test2 services.AddTransient<Test2>(); //把Proxy绑定到根节点 services.AddOptions() //绑定整个配置文件 .Configure<Config>(e => configRoot.Bind(e)) //绑定某个节点 .Configure<Proxy>(e => configRoot.GetSection("proxy").Bind(e)); while (true) { var serviceProvider = services.BuildServiceProvider(); var test = serviceProvider.GetService<Test>(); var test2 = serviceProvider.GetService<Test2>(); test.ok(); test2.ok(); Thread.Sleep(3000); } Console.ReadKey(); public class Proxy { public string Address { get; set; } } public class Config { public string name { get; set; } public int age { get; set; } public Proxy proxy { get; set; } }
Test2.cs
internal class Test2 { private readonly IOptionsMonitor<Config> optionsMonitor; public Test2(IOptionsMonitor<Config> optionsMonitor) { this.optionsMonitor = optionsMonitor; } public void ok() { var config = optionsMonitor.CurrentValue; Console.WriteLine(config.age); } }
3.3 一定范围内会读取配置文件新值(如asp.net core 一次请求)
二、从命令行读取
Microsoft.Extensions.Configuration.CommandLine
class Program { static void Main(string[] args) { ConfigurationBuilder configBuilder = new ConfigurationBuilder(); //设置读取命令行 configBuilder.AddCommandLine(args); IConfigurationRoot config = configBuilder.Build(); string name = config["name"]; Console.WriteLine(name); } }
运行命令行
//命令行可以运行多种方式 ConsoleTest.exe name=tom ConsoleTest.exe --name tom ConsoleTest.exe --name=tom ConsoleTest.exe /name tom ConsoleTest.exe /name=tom
配置系统优先级