配置系统-读取配置3
推荐使用:
1.配合依赖注入进行配置,利用"reloadOnChange"机制
2.Nuget安装:
Install-Package Microsoft.Extensions.Options
Install-Package Microsoft.Extensions.Configuration
Install-Package Microsoft.Extensions.Configuration.Json
Install-Package Microsoft.Extensions.Configuration.Binder
3.在读取配置的时候使用依赖注入Options(下面是读取的即时性:代码执行时配置文件改变了)
IOptions :不会读到变更的值
IOptionsMonitor :即时读取变更的值
IOptionsSnapshot : 一次范围内/一个方法(例如:一次HTTP请求) 值保持不变,如果变更了,则在下次操作读取新的值(推荐)
上代码:新建控制台项目:
-- Config.Json
{ "name": "zhangsan", "age": 18, "classInfo": { "teacher": "Miss Wang", "className": "grade one", "seat": [2,8] } }
-- Demo.cs
internal class Demo { private readonly IOptionsSnapshotoptions; public Demo(IOptionsSnapshot options) { this.options = options; } public void GetStudentInfo() { Console.WriteLine(options.Value.ToString()); } public void GetClassInfo() { Console.WriteLine(options.Value.classInfo.ToString()); } }
-- Program.cs
using DI_ReadConfig; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; ServiceCollection sc=new ServiceCollection(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddJsonFile("Config.json", true/*防止读取出错*/, true/*文件变更重新加载*/);//加载文件 IConfigurationRoot configurationRoot = configurationBuilder.Build(); sc.AddOptions().Configure(s=> configurationRoot.Bind(s)); // 添加配置绑定 sc.AddScoped (); using (ServiceProvider sp = sc.BuildServiceProvider()) { Demo demo= sp.GetRequiredService (); demo.GetStudentInfo(); demo.GetClassInfo(); } public class Student { public string Name { get; set; } public int Age { get; set; } public ClassInfo classInfo { get; set; } public override string ToString() { return $"我是{Name},今年{Age}岁"; } } public class ClassInfo { public string Teacher { get; set; } public string ClassName { get; set; } public int[] Seat { get; set; } public override string ToString() { return $"老师是{Teacher} 班级:{ClassName} 坐在第{Seat[0]}排第{Seat[1]}列"; } }
来看输出:
如果只想读取班级信息怎么办?
新增类Demo2
internal class Demo2 { private readonly IOptionsSnapshot_options; public Demo2(IOptionsSnapshot options) { this._options = options; } public void GetClassInfo() { Console.WriteLine(_options.Value.ToString()); } }
修改-program
ServiceCollection sc=new ServiceCollection(); sc.AddScoped();---------------------------------------------------增加
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("Config.json", true/*防止读取出错*/, true/*文件变更重新加载*/);//加载文件
IConfigurationRoot configurationRoot = configurationBuilder.Build();
sc.AddOptions().Configure(s=> configurationRoot.Bind(s))// 添加配置绑定
.Configure(c=> configurationRoot.GetSection("ClassInfo").Bind(c)); //绑定班级信息----------------------------------------------------增加
using (ServiceProvider sp = sc.BuildServiceProvider())
{
Demo2 demo= sp.GetRequiredService();
demo.GetClassInfo();
}
来看输出:
继续来看看是否会读取最新配置:
新建Winform项目:
--Config.json
复制上面的
--Form1.cs【设计】
--Form1.cs 代码:
public partial class Form1 : Form { private readonly ServiceCollection sc; public Form1() { InitializeComponent(); textBox1.Text = "Config.json"; sc = new ServiceCollection(); sc.AddScoped(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddJsonFile("Config.json", true/*防止读取出错*/, true/*文件变更重新加载*/);//加载文件 IConfigurationRoot configurationRoot = configurationBuilder.Build(); sc.AddOptions().Configure (s => configurationRoot.Bind(s))// 添加配置绑定 .Configure (c => configurationRoot.GetSection("ClassInfo").Bind(c)); //绑定班级信息 } private void button1_Click(object sender, EventArgs e) { using (ServiceProvider sp = sc.BuildServiceProvider()) { Demo2 demo = sp.GetRequiredService (); textBox2.Text= demo.GetClassInfo(); } } } internal class Demo2 { private readonly IOptionsSnapshot _options; public Demo2(IOptionsSnapshot options) { this._options = options; } public string GetClassInfo() { return _options.Value.ToString(); } } public class Student { public string Name { get; set; } public int Age { get; set; } public ClassInfo classInfo { get; set; } public override string ToString() { return $"我是{Name},今年{Age}岁"; } } public class ClassInfo { public string Teacher { get; set; } public string ClassName { get; set; } public int[] Seat { get; set; } public override string ToString() { return $"老师是{Teacher} 班级:{ClassName} 坐在第{Seat[0]}排第{Seat[1]}列"; } }
先点击按钮:查看结果:
去到Bin下面找到Config.json 用文本文件打开:
把2改成12,点击ctro+S 保存,再去点击按钮试试。
想试试及时读取和不读取可以在代码中间增加Thread.Sleep(10000) 之类的代码,在程序阻塞时修改文件进行测试验证效果!