Asp.net core中實(shí)現(xiàn)自動更新的Option的方法示例
Asp.net core可以監(jiān)視json、xml等配置文件的變化, 自動刷新內(nèi)存中的配置內(nèi)容, 但如果想每隔1秒從zookeeper、consul獲取最新的配置信息, 需要自己實(shí)現(xiàn).
閱讀了 Asp.net core Document的Custom configuration provider, 得知只需要實(shí)現(xiàn)自己的IConfigurationSource和對應(yīng)ConfigurationProvider即可
在這個示例中, 我建立了一個簡單的option, 只包含一個不斷變化的計(jì)數(shù)器變量.
public class RefreshableOptions
{
public int IncreasementCount { get; set; }
}
實(shí)現(xiàn)IConfigurationSource和對應(yīng)ConfigurationProvider, 內(nèi)部有一個timer模擬從外部獲取了最新的數(shù)據(jù), 這里為簡單起見, 采用硬編碼的方式指定了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;
}
}
//判斷兩個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());
}
}
使用方法
新建一個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() };
}
}
啟動后不停的刷新http://localhost:5000/api/values可以看到返回內(nèi)容的變化
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net后臺如何輸出js腳本使用什么方法可以實(shí)現(xiàn)
asp.net后臺如何輸出js腳本,用page.ClientScript.RegisterStartupScript方式實(shí)現(xiàn),實(shí)現(xiàn)示例如下,感興趣的朋友不要錯過2014-01-01
.Net使用RabbitMQ實(shí)現(xiàn)短信密碼重置的操作步驟
在C#開發(fā)中,通過RabbitMQ實(shí)現(xiàn)短信服務(wù)可增強(qiáng)應(yīng)用的消息通知能力,本文介紹了使用RabbitMQ發(fā)送短信的步驟,包括安裝RabbitMQ客戶端庫、創(chuàng)建連接和通道、實(shí)現(xiàn)短信發(fā)送服務(wù)、配置RabbitMQ消費(fèi)者,并集成到用戶密碼重置流程中,通過示例代碼,可以快速理解整個實(shí)現(xiàn)過程2024-09-09
asp.net程序編譯調(diào)試時偶爾出現(xiàn)訪問被拒絕的錯誤的解決方法
asp.net程序編譯調(diào)試時偶爾出現(xiàn)訪問被拒絕的錯誤的解決方法...2007-04-04
DataTable轉(zhuǎn)成字符串復(fù)制到txt文本的小例子
DataTable轉(zhuǎn)成字符串復(fù)制到txt文本的小例子,需要的朋友可以參考一下2013-03-03
asp.net 參數(shù)不同共用一個頁面的實(shí)現(xiàn)方法
本文為大家介紹下asp.net參數(shù)不同如何共用一個頁面,感興趣的朋友不要錯過2013-12-12

