{"id":2379,"date":"2021-10-03T00:04:45","date_gmt":"2021-10-02T16:04:45","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2379"},"modified":"2022-02-16T17:32:17","modified_gmt":"2022-02-16T09:32:17","slug":"configuration-ioptionsmonitor","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2021\/10\/03\/configuration-ioptionsmonitor\/","title":{"rendered":"D-12 \u8a2d\u5b9a\u64cb ? configuration ? IOptionsMonitor"},"content":{"rendered":"<h1>\u8a2d\u5b9a\u6a94<\/h1>\n<p>\u6628\u65e5\u8aaa\u660e\u95dc\u65bc\u4f7f\u7528\u8005\u8eab\u5206\u9a57\u8b49\u4ee5\u53ca\u6b0a\u9650\u8a2d\u5b9a\u7684\u90e8\u5206\u52a0\u4ee5\u8aaa\u660e\uff0c\u4e26\u4e14\u900f\u904e\u7b2c\u4e09\u65b9\u63d2\u4ef6\u7684\u65b9\u5f0f\u5c55\u73fe\u5982\u4f55\u5728dotnetcore\u88e1\u9762\u5be6\u4f5cjwt\u7684\u5354\u5b9a\uff0c\u7136\u800c\u4eca\u5929\u6703\u8655\u7406\u751a\u9ebc\u554f\u984c\u5462\uff0c\u8acb\u5927\u5bb6\u770b\u4e0b\u53bb\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u5982\u4f55\u8655\u7406\u8a2d\u5b9a\u6a94<\/h2>\n<p>\u300c\u7cdf\u7cd5\uff0c\u6211\u8981\u600e\u9ebc\u53bb\u628a\u9023\u7dda\u4e32\u62bd\u51fa\u53bb\u7d66SRE\u8a2d\u5b9a\u963f\u3002\u300d<br \/>\n\u4e00\u5927\u65e9\u5c0f\u5149\u5230\u516c\u53f8\u6642\u5c31\u770b\u5230\u5927\u982d\u4f3c\u4e4e\u53c8\u9047\u5230\u4e86\u554f\u984c\uff0c\u53ef\u80fd\u662f\u6628\u5929\u5beb\u7684\u6771\u897f\u9700\u8981\u9023\u7dda\u5230\u8cc7\u6599\u5eab\u4f46\u662f\u6c92\u6709\u5730\u65b9\u53ef\u4ee5\u5b58\u653e\u9023\u7dda\u4e32\u3002<br \/>\n\u300c\u524d\u8f29\u963f\uff0c\u6211\u8981\u628a\u9023\u7dda\u4e32\u653e\u54ea\u88e1\u554a?\u300d<br \/>\n\u9019\u6642\u5019\u5927\u982d\u76f4\u63a5\u8d70\u5411\u8001K\u90a3\u908a\uff0c\u76f4\u63a5\u8ddf\u8001K\u8acb\u6559\u95dc\u65bc\u9023\u7dda\u4e32\u8a72\u5982\u4f55\u8655\u7406\u3002<br \/>\n\u300c\u5f88\u7c21\u55ae\u554a\uff0c\u4f60\u5c31\u653e\u6587\u5b57\u6a94\u5c31\u597d\u4e86\u554a\uff0c\u5230\u6642\u518d\u53bb\u8b80\u53d6\u6587\u5b57\u6a94\u5373\u53ef\u3002\u300d<br \/>\n\u807d\u5230\u8001K\u7684\u8aaa\u660e\u5f8c\u5927\u982d\u4f3c\u4e4e\u6709\u60f3\u6cd5\u7684\u56de\u5230\u5ea7\u4f4d\u4e0a\u6e96\u5099\u958b\u59cb\u958b\u767c\u7684\u6a23\u5b50\uff0c\u9019\u6642\u8001K\u5feb\u6b65\u8d70\u5230\u5927\u982d\u8eab\u908a\u3002<br \/>\n\u300c\u7b49\u7b49~~~\u4f60\u6e96\u5099\u600e\u9ebc\u505a\u963f\uff0c\u61c9\u8a72\u4e0d\u662f\u5beb\u4e00\u500b\u8b80\u6a94\u7684\u529f\u80fd\u7136\u5f8c\u628a\u8cc7\u6599\u653e\u5728\u8a18\u61b6\u9ad4\u88e1\u9762\u5427\u3002\u300d<br \/>\n\u5927\u982d\u7684\u8868\u60c5\u4e00\u526f\u662f\u4f60\u600e\u9ebc\u77e5\u9053\uff0c\u7136\u5f8c\u5c31\u662f\u6709\u751a\u9ebc\u554f\u984c\u55ce\u3002<br \/>\n\u300c\u5509\uff0c\u4f60\u53c8\u60f3\u8981\u81ea\u5df1\u8655\u7406\u8a2d\u5b9a\u8cc7\u6599\u5f97\u5b58\u653e\u8ddf\u52d5\u614b\u52a0\u8f09\u55ce\uff0c\u5176\u5be6\u4f60\u53ea\u8981\u4f7f\u7528IOptionsMonitor\u8ddfConfiguration\u5c31\u641e\u5b9a\u4e86\u3002\u300d<br \/>\n\u4e0d\u904e\u8001K\u8aaa\u4e86\u4e4b\u5f8c\u5c0d\u65bc\u5927\u982d\u800c\u8a00\u597d\u50cf\u751a\u9ebc\u90fd\u6c92\u8aaa\u7684\u6a23\u5b50\uff0c\u6240\u4ee5\u8001K\u5c31\u6372\u8d77\u8896\u5b50\u6e96\u5099\u5927\u5c55\u8eab\u624b\u7684\u6a23\u5b50\u3002<br \/>\n\u300c\u4f86\u5427\u6211\u793a\u7bc4\u7d66\u4f60\u770b\u5427\u3002\u300d<\/p>\n<h2>Configuration<\/h2>\n<p>\u6240\u4ee5\u9996\u5148\u7b2c\u4e00\u52d5\u662f\u5148\u628a\u6a94\u6848\u52a0\u8f09\u9032\u4f86\u653e\u5728\u8a18\u61b6\u9ad4\u88e1\u9762\uff0c\u9019\u90e8\u5206\u5c31\u662fdotnetcore\u5167\u5efa\u7684<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/configuration\/\" title=\"\u8a2d\u5b9a\u6a94\" rel=\"nofollow noopener\" target=\"_blank\">\u8a2d\u5b9a\u6a94<\/a>\u7684\u90e8\u5206\uff0c\u9019\u90e8\u5206\u9996\u5148\u662f\u5728<code>Program.CreateHostBuilder<\/code>\u52a0\u5165\u4ee5\u4e0b\u5167\u5bb9\u3002<\/p>\n<pre><code class=\"language-cs\">public static IWebHostBuilder CreateHostBuilder(string[] args) =&gt;\n    WebHost.CreateDefaultBuilder(args)\n        .UseContentRoot(Directory.GetCurrentDirectory())\n        .ConfigureAppConfiguration((hostingContext, config) =&gt;\n        {\n            config.AddJsonFile(\n                &quot;Settings\/CommonSetting.json&quot;, optional: false, reloadOnChange: true);\n        });<\/code><\/pre>\n<p>\u9996\u5148<code>UseContentRoot<\/code>\u662f\u8a2d\u5b9a\u7db2\u9801\u4f3a\u670d\u5668\u7684\u8ddf\u76ee\u9304\uff0c<code>ConfigureAppConfiguration<\/code>\u50b3\u7684<code>Lambda<\/code>\u5c31\u662f\u8981\u8f09\u5165\u7684\u8a2d\u5b9a\u6a94\uff0c\u5176\u4e2d:<\/p>\n<ul>\n<li>optional -&gt; \u662f\u5426\u70ba\u9078\u7528\uff0c\u5982\u679c\u4e00\u5b9a\u8981\u6709\u9019\u7576\u6848\u5c31\u628a\u4ed6\u8a2d\u6210<code>false<\/code>\u4f46\u662f\u5982\u904e\u4e0d\u9700\u8981\u5f37\u5236\u8b80\u9019\u6a94\u6848\u5c31\u8a2d\u6210<code>true<\/code>\u3002<\/li>\n<li>reloadOnChange -&gt; \u662f\u5426\u652f\u63f4\u52d5\u614b\u52a0\u8f09\uff0c\u5b58\u6a94\u5f8c\u8a18\u61b6\u9ad4\u5167\u6a94\u6848\u5167\u5bb9\u6703\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u4f86\u5982\u679c\u8981\u4f9d\u64da\u74b0\u5883\u4e0d\u540c\u8cc7\u6599\u8981\u4e0d\u540c\u7684\u8a71\u8acb\u770b\u4e0b\u5217\u7bc4\u4f8b\u3002<\/p>\n<pre><code class=\"language-cs\">.ConfigureAppConfiguration((hostingContext, config) =&gt;\n{\n    config.AddJsonFile(\n        &quot;Settings\/CommonSetting.json&quot;, optional: false, reloadOnChange: true);\n    var env = hostingContext.HostingEnvironment;\n    config.AddJsonFile(\n        $&quot;Settings\/CommonSetting.{env.EnvironmentName}.json&quot;, optional: true, reloadOnChange: true);\n});<\/code><\/pre>\n<p>\u5982\u6b64\u53ef\u4ee5\u53d6\u5f97\u74b0\u5883\u8b8a\u6578<code>HostingEnvironment<\/code>\uff0c\u6240\u4ee5\u53ea\u8981\u5728\u591a\u4e00\u500b<code>CommonSetting.SIT.json<\/code>\u5c31\u53ef\u4ee5\u5728SIT\u74b0\u5883\u4f7f\u7528\u4e86\u3002<\/p>\n<h2>IOptionsMonitor<\/h2>\n<p>\u5982\u679c\u4e0d\u4ecb\u610f\u76f4\u63a5\u76f8\u4f9d\u65bcConfiguration\u4ee5\u53ca\u4e0d\u6703\u89ba\u5f97\u53d6\u51fa\u4f86\u8cc7\u6599\u9084\u8981\u89e3\u6790\u7684\u670b\u53cb\u5011\u606d\u559c\u4e86\uff0c\u56e0\u70ba\u672c\u7bc7\u7684\u5167\u5bb9\u5230\u4e0a\u9762\u5c31\u7d50\u675f\u4e86\u3002\u4e0d\u904e\u60f3\u8981\u9700\u8981\u5c07\u8a2d\u5b9a\u6a94\u89e3\u6790\u6210\u985e\u5225\u7269\u4ef6\u7684\u5c31\u7e7c\u7e8c\u770b\u4e0b\u53bb\u3002<br \/>\n\u63a5\u4e0b\u4f86\u8981\u8aaa\u660e<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/aspnet\/core\/fundamentals\/configuration\/options\" title=\"\u9078\u9805\u6a21\u5f0f\" rel=\"nofollow noopener\" target=\"_blank\">\u9078\u9805\u6a21\u5f0f<\/a>\uff0c\u5728\u7e7c\u7e8c\u4e0b\u53bb\u4e4b\u524d\u8981\u8acb\u5927\u5bb6\u5148\u5b89\u88dd\u4e0b\u9762\u7684\u5957\u4ef6\u3002<\/p>\n<pre><code class=\"language-bash\">dotnet add package Microsoft.Extensions.Options<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>\u7136\u5f8c\u63a5\u4e0b\u4f86\u8981\u5148\u8aaa\u660e\u9078\u9805\u6a21\u5f0f\u4e2d\u7684\u4e09\u7a2e\u6a21\u5f0f<\/th>\n<th><\/th>\n<th><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>IOptions<TOptions><\/td>\n<td>\u6a94\u6848\u8b8a\u52d5\u6642\u8cc7\u6599\u4e0d\u6703\u8ddf\u8457\u8b8a\u52d5<\/td>\n<\/tr>\n<tr>\n<td>IOptionsSnapshot<TOptions><\/td>\n<td>\u6ce8\u5165\u4e4b\u5f8c\u5167\u5bb9\u5c31\u4e0d\u6703\u8b8a\u52d5\uff0c\u4f46\u662f\u6a94\u6848\u7570\u52d5\u5f8c\u6240\u6ce8\u5165\u7684\u8cc7\u6599\u5c31\u662f\u7570\u52d5\u5f8c\u7684\u8cc7\u6599<\/td>\n<\/tr>\n<tr>\n<td>IOptionsMonitor<TOptions><\/td>\n<td>\u652f\u63f4\u71b1\u91cd\u8f09\uff0c\u53ef\u4ee5\u9069\u7528\u5728\u55ae\u4f8b(Singleton)\u6a21\u5f0f\u7684\u7269\u4ef6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u8aaa\u660e\u5b8c\u7a2e\u985e\u5f8c\u73fe\u5728\u8981\u958b\u59cb\u8aaa\u660e\u5982\u4f55\u628a\u8cc7\u6599\u653e\u5230\u9078\u9805\u6a21\u5f0f\u7269\u4ef6\u4e2d\u3002<\/p>\n<h3>Configuration\u8ddfIOptionsMonitor\u7684\u6a4b\u63a5<\/h3>\n<p>\u9019\u90e8\u5206\u8aaa\u660e\u600e\u9ebc\u628a\u8cc7\u6599\u5f9eConfiguration\u8a3b\u518a\u5230IOptionsMonitor\u7269\u4ef6\u4e2d\uff0c<\/p>\n<p>\u9996\u5148\u6211\u5011\u5148\u5b9a\u7fa9\u4e00\u500b\u985e\u5225\u5982\u4e0b\uff0c<\/p>\n<pre><code class=\"language-cs\">public class CommonSetting\n{\n    public string Connection { get; set; }\n}<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\u8981\u518d<code>Startup.ConfigureServices<\/code>\u52a0\u5165\u4ee5\u4e0b\u5167\u5bb9\u3002<\/p>\n<pre><code class=\"language-cs\">services.AddOptions();\nservices.Configure&lt;CommonSetting&gt;(Configuration.GetSection(&quot;CommonSetting&quot;));\nservices.Configure&lt;List&lt;CommonSetting&gt;&gt;(Configuration.GetSection(&quot;CommonSettings&quot;));<\/code><\/pre>\n<p>\u6240\u4ee5\u7576\u4f60\u7684\u8a2d\u5b9a\u6a94\u4e2d\u6709\u4ee5\u4e0b\u5167\u5bb9\u5c31\u53ef\u4ee5\u53d6\u5f97\u8cc7\u6599\u4e26\u6210\u529f\u6ce8\u5165<\/p>\n<pre><code class=\"language-json\">{\n  &quot;CommonSetting&quot;:{\n      &quot;Connections&quot;:&quot;.....&quot;\n  }\n  &quot;CommonSettings&quot;: [{\n       &quot;Connections&quot;:&quot;.....&quot;\n     },{\n       &quot;Connections&quot;:&quot;.....&quot;\n     }\n  ],\n}<\/code><\/pre>\n<p>\u6700\u5f8c\u4ee5IOptionsMonitor\u70ba\u4f8b\u8aaa\u660e\u5982\u4f55\u5728\u7269\u4ef6\u6642\u4f8b\u4e2d\u4f7f\u7528\uff0c\u8acb\u5148\u770b\u4ee5\u4e0b\u8aaa\u660e\u3002<\/p>\n<pre><code class=\"language-cs\">public class HomeController : Controller\n{\n    private readonly IOptionsMonitor&lt;CommonSetting&gt; _setting;\n\n    private readonly IOptionsMonitor&lt;List&lt;CommonSetting&gt;&gt; _settings;\n\n    public HomeController(IOptionsMonitor&lt;CommonSetting&gt; setting,\n        IOptionsMonitor&lt;List&lt;CommonSetting&gt;&gt; settings)\n    {\n        setting = _setting;\n        settings = _settings;\n    }\n\n    public IActionResult Index()\n    {\n        _setting.CurrentValue.Connection;\n        foreach(var item in _settings.CurrentValue)\n        {\n        }\n    }\n}<\/code><\/pre>\n<p>\u7c21\u55ae\u8aaa\u660e\u5c31\u662f\u900f\u904e\u5efa\u69cb\u5b50\u6ce8\u5165\u4e4b\u5f8c\u4f7f\u7528<code>.CurrentValue<\/code>\u53ef\u4ee5\u53d6\u7684\u6700\u65b0\u7684\u8cc7\u6599\u4e26\u505a\u5c0d\u61c9\u7684\u4f7f\u7528\u5373\u53ef\u3002<\/p>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u4eca\u5929\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u679c\u6709\u9700\u8981\u4f7f\u7528\u8a2d\u5b9a\u6a94\u8a72\u5982\u4f55\u900f\u904e<code>Microsoft.Extensions.Options<\/code>\u9019\u500b\u5957\u4ef6\u4f86\u89e3\u6790\u8a2d\u5b9a\u6a94\u4e26\u4e14\u7576\u8a2d\u5b9a\u6a94\u7570\u52d5\u6642\u53ef\u4ee5\u53d6\u5f97\u7570\u52d5\u5f8c\u7684\u8cc7\u8a0a\uff0c\u5e0c\u671b\u80fd\u5c0d\u5927\u5bb6\u7684\u958b\u767c\u6709\u6240\u5e6b\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8a2d\u5b9a\u6a94 \u6628\u65e5\u8aaa\u660e\u95dc\u65bc\u4f7f\u7528\u8005\u8eab\u5206\u9a57\u8b49\u4ee5\u53ca\u6b0a\u9650\u8a2d\u5b9a\u7684\u90e8\u5206\u52a0\u4ee5\u8aaa\u660e\uff0c\u4e26\u4e14\u900f\u904e\u7b2c\u4e09\u65b9\u63d2\u4ef6\u7684\u65b9\u5f0f\u5c55\u73fe\u5982\u4f55\u5728dotnetc &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2021\/10\/03\/configuration-ioptionsmonitor\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008D-12 \u8a2d\u5b9a\u64cb ? configuration ? IOptionsMonitor\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[90,126,92],"class_list":["post-2379","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2021ironman","tag-configuration","tag-dotnetcore"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2379"}],"version-history":[{"count":16,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2379\/revisions"}],"predecessor-version":[{"id":2935,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2379\/revisions\/2935"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}