Asp.net core中實(shí)現(xiàn)自動(dòng)更新的Option的方法示例
Asp.net core可以監(jiān)視json、xml等配置文件的變化, 自動(dòng)刷新內(nèi)存中的配置內(nèi)容, 但如果想每隔1秒從zookeeper、consul獲取最新的配置信息, 需要自己實(shí)現(xiàn).
閱讀了 Asp.net core Document的Custom configuration provider, 得知只需要實(shí)現(xiàn)自己的IConfigurationSource和對(duì)應(yīng)ConfigurationProvider即可
在這個(gè)示例中, 我建立了一個(gè)簡(jiǎn)單的option, 只包含一個(gè)不斷變化的計(jì)數(shù)器變量.
public class RefreshableOptions { public int IncreasementCount { get; set; } }
實(shí)現(xiàn)IConfigurationSource和對(duì)應(yīng)ConfigurationProvider, 內(nèi)部有一個(gè)timer模擬從外部獲取了最新的數(shù)據(jù), 這里為簡(jiǎn)單起見, 采用硬編碼的方式指定了option的路徑
public class AutoRefreshConfigurationSource : IConfigurationSource { public IConfigurationProvider Build(IConfigurationBuilder builder) { return new AutoRefreshConfigurationProvider(); } } public class AutoRefreshConfigurationProvider : ConfigurationProvider { private int count = 0; private bool isChanged; public AutoRefreshConfigurationProvider() : base() { Timer timer = new Timer(TimerCallback); timer.Change(1000, 3000); } public override void Load() { var beforeData = Data; // 這里采用硬編碼指定option的路徑 Data = new Dictionary<string, string>() { { "AutoRefreshOptions:IncreasementCount", count.ToString() } }; isChanged = IsDictionaryChanged(beforeData, Data); } private void TimerCallback(object state) { count++; this.Load(); if (isChanged) { base.OnReload();//通知IConfiguration實(shí)例, 有參數(shù)發(fā)生了改變 isChanged = false; } } //判斷兩個(gè)Idictionary是否有不同的幫助方法 private static bool IsDictionaryChanged(IDictionary<string, string> before, IDictionary<string, string> after) { if (before == null && after == null) { return false; } if ((before == null) != (after == null)) { return true; } if (before.Count != after.Count) { return true; } var ignoreCaseBefore = new Dictionary<string, string>(before, StringComparer.OrdinalIgnoreCase); foreach (var afterItemKey in after.Keys) { if (!ignoreCaseBefore.TryGetValue(afterItemKey, out var beforeItemValue)) { return true; } if (beforeItemValue != after[afterItemKey]) { return true; } ignoreCaseBefore.Remove(afterItemKey); } if (ignoreCaseBefore.Count > 0) { return true; } return false; } }
實(shí)現(xiàn)擴(kuò)展方法
public static class AutoRereshConfigurationExtensions { public static IConfigurationBuilder AddAutoRereshConfiguration(this IConfigurationBuilder builder) { return builder.Add(new AutoRefreshConfigurationSource()); } }
使用方法
新建一個(gè)WebApi項(xiàng)目, 在Program.CreateWebHostBuilder中增加黃色部分
WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(config => { config.AddAutoRereshConfiguration(); }) .UseStartup<Startup>();
在Startup. ConfigureServices中配置
services.Configure<RefreshableOptions>(Configuration.GetSection("AutoRefreshOptions"));
修改ValuesController
[Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { private RefreshableOptions refreshableOptions; public ValuesController(IOptionsSnapshot<RefreshableOptions> refreshableOptions) { this.refreshableOptions = refreshableOptions.Value; } [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "value1", "value2", refreshableOptions.IncreasementCount.ToString() }; } }
啟動(dòng)后不停的刷新http://localhost:5000/api/values可以看到返回內(nèi)容的變化
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Asp.net Mvc 身份驗(yàn)證、異常處理、權(quán)限驗(yàn)證(攔截器)實(shí)現(xiàn)代碼
本問主要介紹asp.net的身份驗(yàn)證機(jī)制及asp.net MVC攔截器在項(xiàng)目中的運(yùn)用?,F(xiàn)在讓我們來模擬一個(gè)簡(jiǎn)單的流程:用戶登錄》權(quán)限驗(yàn)證》異常處理2012-10-10linq to sql 中,如何解決多條件查詢問題,答案,用表達(dá)式樹! (下)
在上一篇中,我們做了基于linq to sql 的多條件組合查詢,但通過監(jiān)視數(shù)據(jù)庫(kù)發(fā)現(xiàn),這樣做的成本比較高,每次都要取出全部的數(shù)據(jù)到內(nèi)存進(jìn)行篩選.2011-08-08ClickOnce DIY全自動(dòng)更新下載升級(jí)的自我實(shí)現(xiàn)
ClickOnce DIY全自動(dòng)更新下載升級(jí)的自我實(shí)現(xiàn)...2007-08-08ASP.NET中用js取CheckBoxList中值的方法實(shí)例
用腳本取CheckBoxList中的值,并用"|"將其分開,之后將取到的值放入文本框,返回?cái)?shù)據(jù)庫(kù)做添加或者修改2013-07-07ASP.NET中實(shí)現(xiàn)文件的保護(hù)性下載基礎(chǔ)篇
許多時(shí)候,我們需要在因特網(wǎng)上提供文件下載服務(wù),但是又要防止未經(jīng)授權(quán)的下載,這時(shí)該怎么辦?本文將為讀者詳細(xì)介紹一種使用ASP.NET實(shí)現(xiàn)的HTTP處理程序的解決方案。2011-02-02.NET實(shí)現(xiàn)魔方游戲(一)之任意階魔方的表示
這篇文章主要介紹了.NET實(shí)現(xiàn)魔方游戲(一)之任意階魔方的表示 的相關(guān)資料,需要的朋友可以參考下2016-02-02asp.net下結(jié)合HttpHandler實(shí)現(xiàn)圖片防盜鏈
asp.net防圖片盜鏈HttpHandler2010-07-07