dotnetcore配置框架简介
2021-03-13 14:31
标签:class on() dcom optional 接口 目的 ade dad cname 配置的本质就是字符串的键值对,微软的一系列接口其实就是对这些键值对字符串的抽象。 注:各种配置方式的目的是在控制台中输出以下内容,如下图:
更多精彩文章,请关注我的公众号:
dotnetcore配置框架简介 标签:class on() dcom optional 接口 目的 ade dad cname 原文地址:https://www.cnblogs.com/liuzhenbao/p/12818806.html一、前言
二、基本类型
2.1、Nuget包
2.2、抽象接口
三、基本配置
3.1、加载内存中的配置
static void Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder();
var memoryData = new Dictionarystring, string>();
memoryData.Add("Student:Code", "10002385");
memoryData.Add("Student:Name", "LiuSan");
builder.AddInMemoryCollection(memoryData);
IConfigurationRoot configurationRoot = builder.Build();
IConfigurationSection configurationSection = configurationRoot.GetSection("Student");
Console.WriteLine($"Code:{configurationSection["Code"]}");
Console.WriteLine($"Name:{configurationSection["Name"]}");
Console.Read();
}
3.2、加载环境变量中的配置
1 static void Main(string[] args)
2 {
3 IConfigurationBuilder builder = new ConfigurationBuilder();
4 builder.AddEnvironmentVariables();
5 IConfigurationRoot configurationRoot = builder.Build();
6 IConfigurationSection configurationSection = configurationRoot.GetSection("Student");
7
8 Console.WriteLine($"Code:{configurationSection["Code"]}");
9 Console.WriteLine($"Name:{configurationSection["Name"]}");
10
11 Console.Read();
12 }
3.3、加载命令行中的配置
1 static void Main(string[] args)
2 {
3 IConfigurationBuilder builder = new ConfigurationBuilder();
4 builder.AddCommandLine(args);
5 IConfigurationRoot configurationRoot = builder.Build();
6 IConfigurationSection configurationSection = configurationRoot.GetSection("Student");
7
8 Console.WriteLine($"Code:{configurationSection["Code"]}");
9 Console.WriteLine($"Name:{configurationSection["Name"]}");
10
11 Console.Read();
12 }
3.4、加载Json中的配置
1 static void Main(string[] args)
2 {
3 IConfigurationBuilder builder = new ConfigurationBuilder();
4 builder.AddJsonFile("appsettings.json");
5 IConfigurationRoot configurationRoot = builder.Build();
6 IConfigurationSection configurationSection = configurationRoot.GetSection("Student");
7
8 Console.WriteLine($"Code:{configurationSection["Code"]}");
9 Console.WriteLine($"Name:{configurationSection["Name"]}");
10
11 Console.Read();
12 }
3.5、加载xml中的配置
1 static void Main(string[] args)
2 {
3 IConfigurationBuilder builder = new ConfigurationBuilder();
4 builder.AddXmlFile("appsettings.xml");
5 IConfigurationRoot configurationRoot = builder.Build();
6 IConfigurationSection configurationSection = configurationRoot.GetSection("Student");
7
8 Console.WriteLine($"Code:{configurationSection["Code"]}");
9 Console.WriteLine($"Name:{configurationSection["Name"]}");
10
11 Console.Read();
12 }
四、高级配置
4.1、将配置绑定至强对象
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 IConfigurationBuilder builder = new ConfigurationBuilder();
6 builder.AddJsonFile("appsettings.json");
7 IConfigurationRoot configurationRoot = builder.Build();
8
9 var student = new Student();
10 configurationRoot.GetSection("Student").Bind(student);
11
12 Console.WriteLine($"Code:{student.Code}");
13 Console.WriteLine($"Name:{student.Name}");
14
15 Console.Read();
16 }
17 }
18
19 class Student
20 {
21 public string Code { get; set; }
22
23 public string Name { get; set; }
24 }
4.2、IOptions
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 IConfigurationBuilder builder = new ConfigurationBuilder();
6 builder.AddJsonFile("appsettings.json");
7 IConfigurationRoot configurationRoot = builder.Build();
8
9 IServiceCollection services = new ServiceCollection();
10 services.Configure
4.3、热更新
1 static void Main(string[] args)
2 {
3 IConfigurationBuilder builder = new ConfigurationBuilder();
4 builder.AddJsonFile("appsettings.json", optional:false, reloadOnChange:true);
5 IConfigurationRoot configurationRoot = builder.Build();
6 IConfigurationSection configurationSection = configurationRoot.GetSection("Student");
7
8 var token = configurationRoot.GetReloadToken();
9 ChangeToken.OnChange(() => configurationRoot.GetReloadToken(), () =>
10 {
11 Console.WriteLine("配置发生改变了...");
12 Console.WriteLine($"Code:{configurationSection["Code"]}");
13 Console.WriteLine($"Name:{configurationSection["Name"]}");
14 });
15
16 Console.Read();
17 }
五、总结
上一篇:DotNet浅用Quartz
下一篇:阿里系的几个电商网站