ASP.NET Core 2.2 基础知识(七) 选项模式

承接上一篇 配置,

选项模式是专门用类来表示相关配置的服务.

基本选项配置

新建一个选项类,该类必须是包含无参数的构造函数的非抽象类.

    public class MyOptions
    {
        public MyOptions()
        {
            Id = 1;
            Name = "refuge";
        }

        public int Id { get; set; }

        public string Name { get; set; }
    }

添加到服务容器,并绑定到配置:

    public class Startup
    {
        ......
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.Configure<MyOptions>(Configuration);
        }
        ......
    }

测试控制器:

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IOptions<MyOptions> _options;

        public TestController(IOptions<MyOptions> options)
        {
            _options = options;
            _config = config;
        }

        public MyOptions Get()
        {
            return _options.Value;
        }
    }

结果:

 

通过委托配置选项

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<MyOptions>(options =>
            {
                //这些值会替代 MyOptions 构造函数中设置的值.
                //原因很简单,肯定是对象创建了,才会执行这个委托.
                options.Id = 2;
                options.Name = "refuge2";
            });
        }

 

 

通过文件配置简单选项

新建一个json文件:

{
  "Id": 3,
  "Name": "refuge3"
}

 

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            ConfigurationBuilder builder = new ConfigurationBuilder();
            IConfigurationRoot config = builder.AddJsonFile(path: @"E:\Git\Source\Repos\MyOption\Demo1\jsonconfig.json", optional: true, reloadOnChange: true).Build();
            services.Configure<MyOptions>(config);
        }

 

还可以读取文件中的某个节点配置简单选项

修改 json 文件,增加 subsection 节点.

{
  "Id": 3,
  "Name": "refuge3",
  "SubSection": {
    "Id": 4,
    "Name": "refuge4"
  } 
}

 

        public void ConfigureServices(IServiceCollection services)
        {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            IConfigurationRoot config = builder.AddJsonFile(@"E:\Git\Source\Repos\MyOption\Demo1\jsonconfig.json", true, true).Build();
            services.Configure<MyOptions>(config.GetSection("subsection"));
        }

 

 

上面这些例子中的获取到的 MyOptions 对象都是单例的.比如通过文件配置简单选项,即使 AddJsonFile 方法最后一个参数是 true ,配置数据也不会更新.要实现更新需要使用下面这个接口.

通过 IOptionsSnapshot 重新加载配置数据

修改 TestController :

[Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IOptionsSnapshot<MyOptions> _options;

        public TestController(IOptionsSnapshot<MyOptions> options)
        {
            _options = options;           
        }
        public MyOptions Get()
        {
            return _options.Value;
        }
}

 

第一次请求:

 

修改 json 文件后,刷新页面

 

通过别名配置选项

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.Configure<MyOptions>("option1", Configuration);
            services.Configure<MyOptions>("option2", options =>
            {
                options.Id = 6;
                options.Name = "refuge6";
            });
        }

 

修改 TestController 

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IOptionsSnapshot<MyOptions> _options;

        public TestController(IOptionsSnapshot<MyOptions> options)
        {
            _options = options;
        }

        public IEnumerable<MyOptions> Get()
        {
            for (int i = 1; i < 3; i++)
            {
                yield return _options.Get("option" + i);
            }
        }
    }

 

 

当启用多个配置服务时,指定的最后一个配置源优于其他源.

 

未完待续...

posted @ 2019-01-05 21:06  热敷哥  阅读(1184)  评论(0编辑  收藏  举报