您好,登錄后才能下訂單哦!
如何在Asp.net core中實(shí)現(xiàn)自動(dòng)更新Option?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
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)單起見(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() }; } }
ASP.NET 是開(kāi)源,跨平臺(tái),高性能,輕量級(jí)的 Web 應(yīng)用構(gòu)建框架,常用于通過(guò) HTML、CSS、JavaScript 以及服務(wù)器腳本來(lái)構(gòu)建網(wǎng)頁(yè)和網(wǎng)站。
看完上述內(nèi)容,你們掌握如何在Asp.net core中實(shí)現(xiàn)自動(dòng)更新Option的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。