承接上一篇 配置,

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

基本选项配置

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

    public class MyOptions
{
public MyOptions()
{
Id = ;
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 = ;
options.Name = "refuge2";
});
}

通过文件配置简单选项

新建一个json文件:

{
"Id": ,
"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": ,
"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 = ; i < ; i++)
{
yield return _options.Get("option" + i);
}
}
}

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

未完待续...

ASP.NET Core 2.2 基础知识(七) 选项模式的更多相关文章

  1. ASP.NET Core 2.2 基础知识(十八) 托管和部署 概述

    为了方便演示,以 .NET Core 控制台应用程序讲解. 我们新建一个控制台应用程序,安装 "Newtonsoft.Json" Nuget 包,然后右键点击该项目,选择" ...

  2. ASP.NET Core 2.2 基础知识(十二) 发送 HTTP 请求

    可以注册 IHttpClientFactory 并将其用于配置和创建应用中的 HttpClient 实例. 这能带来以下好处: 提供一个中心位置,用于命名和配置逻辑 HttpClient 实例. 例如 ...

  3. ASP.NET Core 2.2 基础知识(六) 配置(内含MySql+EF)

    先上一段代码,了解一下 .NET Core 配置数据的结构. 新建一个 控制台项目,添加一个文件 json.json ,文件内容如下: { "country": "cn& ...

  4. ASP.NET Core 2.2 基础知识(十六) SignalR 概述

    我一直觉得学习的最好方法就是先让程序能够正常运行,才去学习他的原理,剖析他的细节. 就好像这个图: 所以,我们先跟着官方文档,创建一个 SignalR 应用: https://docs.microso ...

  5. ASP.NET Core 2.2 基础知识(十四) WebAPI Action返回类型(未完待续)

    要啥自行车,直接看手表 //返回基元类型 public string Get() { return "hello world"; } //返回复杂类型 public Person ...

  6. ASP.NET Core 2.2 基础知识(十三) WebAPI 概述

    我们先创建一个 WebAPI 项目,看看官方给的模板到底有哪些东西 官方给出的模板: [Route("api/[controller]")] [ApiController] pub ...

  7. ASP.NET Core 2.2 基础知识(十一) ASP.NET Core 模块

    ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...

  8. ASP.NET Core 2.2 基础知识(十) Web服务器 - Kestrel

    ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...

  9. ASP.NET Core 2.2 基础知识(九) 使用托管服务实现后台任务

    在 ASP.NET Core 中,后台任务作为托管服务实现.托管服务是一个类,而且必须实现 IHostedService 接口,该接口定义了两个方法: StartAsync(CancellationT ...

随机推荐

  1. [洛谷P1536]村村通

    题意:多组数据,当n为0时结束,每组数据表示有n个村子,m条路,求还需要建多少条路,使得所有的村子联通题解:用并查集求出有多少个联通块,然后求解 C++ Code: #include<cstdi ...

  2. SetLocalTime设置本地时间

    /***************************************************************** 函数名:EnableSetTimePriviledge 功 能:开 ...

  3. 【CF MEMSQL 3.0 B. Lazy Security Guard】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Ubuntu下安装LNMP之Mysql的安装及卸载

    Mysql的安装过程也可参考:http://blog.csdn.net/qq_20565303/article/details/69813868 Mysql安装包下载地址:https://dev.my ...

  5. tyvj1305 最大子序和(单调队列

    题目地址:http://www.joyoi.cn/problem/tyvj-1305 最大子序和 题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Loc ...

  6. Linux内存 性能调优

    内存是影响Linux性能的主要因素之一,内存资源的充足与否直接影响应用系统的使用性能. free命令:监控Linux内存使用状况. 由上图可知,空闲内存是free+buffers+cached=155 ...

  7. mysql__索引的设计和使用

    索引的设计和使用 1 索引概述 MySIAM和InnoDB存储引擎的表默认创建的都是BTREE索引,MySQL目前不支持函数索引,但是支持前缀索引.还支持全文本索引,但是只有MySIAM(5.0开始) ...

  8. 理解PHP链式调用

    php链式操作:类似如下实现 $db->where()->limit()->order(); 不使用链式调用时的代码格式如下: namespace Database; class D ...

  9. -webkit-overflow-scrolling:touch;

    -webkit-overflow-scrolling建了带有硬件加速的系统级控件,所以效率很高.但是这相对是耗更多内存的,最好在产生了非常大面积的overflow时才应用. 而且在 ios8  里有b ...

  10. [洛谷P3942] 将军令

    洛谷题目链接:将军令 题目背景 历史/落在/赢家/之手 至少/我们/拥有/传说 谁说/败者/无法/不朽 拳头/只能/让人/低头 念头/却能/让人/抬头 抬头/去看/去爱/去追 你心中的梦 题目描述 又 ...