博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET 云原生架构师训练营(模块二 基础巩固 配置)--学习笔记
阅读量:4036 次
发布时间:2019-05-24

本文共 5919 字,大约阅读时间需要 19 分钟。

2.2.3 核心模块--配置

  • IConfiguration

  • Options

ASP.NET Core 中的配置:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0

IConfiguration

  • IConfiguration 的使用

  • 层级对象配置到 key-value 键值对转换

  • 通过环境变量修改日志级别

  • 通过命令行修改日志级别

IConfiguration 的使用

appsettings.json

{  "Logging": {    "LogLevel": {      "Default": "Information",      "Microsoft": "Warning",      "Microsoft.Hosting.Lifetime": "Information"    }  },  "AllowedHosts": "*"}

新增 ConfigController.cs

namespace HelloApi.Controllers{    [ApiController]    [Route("[controller]")]    public class ConfigController : Controller    {        private readonly IConfiguration _configuration;        public ConfigController(IConfiguration configuration)        {            _configuration = configuration;        }        [HttpGet]        public IActionResult GetConfigurations()        {            var result = new List
(); foreach (var key in _configuration.AsEnumerable()) { result.Add($"Key: {key.Key}, value: {key.Value}"); } return Ok(result); } }}

启动程序,访问:https://localhost:5001/config

不仅得到 appsettings.json 的配置, 还可以得到环境变量配置

可以在 ConfigureAppConfiguration 中清除所有配置,再添加自己需要的配置,后面添加的配置会覆盖前面的配置

.ConfigureAppConfiguration((hostingContext, config) =>{    config.Sources.Clear();    var env = hostingContext.HostingEnvironment;    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);})

启动程序,访问:https://localhost:5001/config

这样可以得到自己添加的配置

层级对象配置到 key-value 键值对转换

appsettings.json

{  "Logging": {    "LogLevel": {      "Default": "Information",      "Microsoft": "Warning",      "Microsoft.Hosting.Lifetime": "Information"    }  },  "AllowedHosts": "*"}

通过冒号读取下一级配置(Windows),Linux 上通过下划线

[HttpGet]public IActionResult GetConfigurations(){    var result = new List
(); //foreach (var key in _configuration.AsEnumerable()) //{ // result.Add($"Key: {key.Key}, value: {key.Value}"); //} return Content(string.Format("Default Log Level: {0}", _configuration["Logging:LogLevel:Default"]));}

启动程序,输出如下:

Default Log Level: Debug

通过环境变量修改日志级别

在 launcSettings.json 中添加 Trace 日志级别

"environmentVariables": {        "ASPNETCORE_ENVIRONMENT": "Development",        "Logging__LogLevel__Default": "Trace"      }

在 CreateHostBuilder 的时候添加环境变量配置

config.AddEnvironmentVariables();

启动程序,输出如下:

Default Log Level: Trace

通过命令行修改日志级别

CreateHostBuilder

config.AddCommandLine(source =>{    source.Args = args;});

在命令行中设置

set Logging__LogLevel__Default=Warning

Options

  • 通过 ConfigurationBinder 操作 Options

  • 通过 Configure 绑定 Option

通过 ConfigurationBinder 操作 Options

新建 MyOption.cs

namespace HelloApi{    public class MyOption    {        public string Name { get; set; }        public int Age { get; set; }    }}

在 appsettings.json 中新增一个节点

"MyOption": {    "Name": "Mingson",    "Age": 25   },

在 ConfigureServices 中绑定

var myOption = new MyOption();Configuration.GetSection("MyOption").Bind(myOption);// 单例注入到全局中services.AddSingleton(myOption);

在 ConfigController 中注入,与获取

private readonly MyOption _myOption;public ConfigController(IConfiguration configuration, MyOption myOption){    _configuration = configuration;    _myOption = myOption;}[HttpGet("option")]public IActionResult GetOption(){    return Ok(_myOption);}

启动程序,访问:https://localhost:5001/config/option

输出如下:

{"name":"Mingson","age":25}

通过 Get 的方式

myOption = Configuration.GetSection("MyOption").Get
();

通过 Configure 绑定 Option

IOptions

  • IOptions

     被注册为 singletone,不支持为可命名的配置
  • IOptionsSnapshot

     被注册为 scoped,支持为可命名的配置
  • IOptionsMonitor

     被注册为 singletone,会被通知,支持重载配置,支持为可命名的配置

IOptions

// 直接注入到容器中services.Configure
(Configuration.GetSection("MyOption"));

通过 IOptions 注入

public ConfigController(IConfiguration configuration, IOptions
myOption){ _configuration = configuration; _myOption = myOption.Value;}

启动程序可以得到同样的输出

IOptionsSnapshot

public ConfigController(IConfiguration configuration, IOptionsSnapshot
myOption){ _configuration = configuration; _myOption = myOption.Value;}

启动程序,修改配置,刷新浏览器,可以获取到修改后的配置

可命名的配置

appsettings.json

"Jack": {    "Name": "Jack",    "Age": 16  },  "Peter": {    "Name": "Peter",    "Age": 18  }

MyOption.cs

public const string PETER = "Peter";public const string JACK = "Jack";

ConfigureServices

services.Configure
("Peter", Configuration.GetSection("Peter"));services.Configure
("Jack", Configuration.GetSection("Jack"));

ConfigController

_myOption = myOption.Get(MyOption.PETER);_myOption = myOption.Get(MyOption.JACK);

启动程序即可读取不同命名的配置

IOptionsMonitor

public ConfigController(IConfiguration configuration, IOptionsMonitor
myOption){ _configuration = configuration; _myOption = myOption.CurrentValue; // 配置变化处理 myOption.OnChange(option => { });}

option 验证

属性验证标签

namespace HelloApi{    public class MyConfigOptions    {        public const string MyConfig = "MyConfig";        [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]        public string Key1 { get; set; }        [Range(0, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]        public int Key2 { get; set; }        public int Key3 { get; set; }    }}

绑定后校验

ConfigureServices

services.AddOptions
().Bind(Configuration.GetSection("MyOption")).ValidateDataAnnotations();

MyOption

[Range(1, 20)]public int Age { get; set; }

启动程序,输出如下:

OptionsValidationException: DataAnnotation validation failed for members: 'Age' with the error: 'The field Age must be between 1 and 20.'.

PostConfigure

当配置被读取出来的时候会被执行

services.PostConfigure
(option =>{ if (option.Age == 20) { option.Age = 19; }});

GitHub源码链接:

https://github.com/MINGSON666/Personal-Learning-Library/tree/main/ArchitectTrainingCamp/HelloApi

课程链接

欢迎各位读者加入微信群一起学习交流,

在公众号后台回复“加群”即可~~

转载地址:http://agkdi.baihongyu.com/

你可能感兴趣的文章
可扩展、高可用服务网络设计方案
查看>>
如何构建高扩展性网站
查看>>
微服务架构的设计模式
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
c++字符数组和字符指针区别以及str***函数
查看>>
c++类的操作符重载注意事项
查看>>
c++模板与泛型编程
查看>>
STL::deque以及由其实现的queue和stack
查看>>
WAV文件解析
查看>>
DAC输出音乐2-解决pu pu 声
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
WPF UI&控件免费开源库
查看>>
QT打开项目提示no valid settings file could be found
查看>>
Win10+VS+ESP32环境搭建
查看>>
Ubuntu+win10远程桌面
查看>>
flutter-实现圆角带边框的view(android无效)
查看>>
android 代码实现圆角
查看>>
flutter-解析json
查看>>
android中shader的使用
查看>>