标签:class on() dcom optional 接口 目的 ade dad cname
配置的本质就是字符串的键值对,微软的一系列接口其实就是对这些键值对字符串的抽象。
注:各种配置方式的目的是在控制台中输出以下内容,如下图:
static void Main(string[] args) { IConfigurationBuilder builder = new ConfigurationBuilder(); var memoryData = new Dictionary<string, 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(); }
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 }
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 }
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 }
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 }
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 }
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<WeixinOptions>(configurationRoot.GetSection("WeixinSetting")); 11 services.AddScoped<IWeixinService, WeixinService>(); 12 13 var provider = services.BuildServiceProvider(); 14 var weixinService = provider.GetService<IWeixinService>(); 15 string appId = weixinService.AppId; 16 string token = weixinService.Token; 17 18 Console.Read(); 19 } 20 } 21 22 interface IWeixinService 23 { 24 string AppId { get; } 25 string Token { get; } 26 } 27 28 class WeixinService: IWeixinService 29 { 30 IOptions<WeixinOptions> _options; 31 public WeixinService(IOptions<WeixinOptions> options) 32 { 33 _options = options; 34 } 35 36 public string AppId { get => _options.Value.AppId; } 37 public string Token { get => _options.Value.Token;} 38 } 39 40 class WeixinOptions 41 { 42 public string AppId { get; set; } 43 44 public string Token { get; set; } 45 }
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 }
更多精彩文章,请关注我的公众号:
标签:class on() dcom optional 接口 目的 ade dad cname
原文地址:https://www.cnblogs.com/liuzhenbao/p/12818806.html