ASP.NET Core中修改配置文件后自動加載新配置的方法詳解
前言
在 ASP.NET Core 默認的應(yīng)用程序模板中, 配置文件的處理如下面的代碼所示:
config.AddJsonFile(
path: "appsettings.json",
optional: true,
reloadOnChange: true
);
config.AddJsonFile(
path: $"appsettings.{env.EnvironmentName}.json",
optional: true,
reloadOnChange: true
);
appsettings.json 和 appsettings.{env.EnvironmentName}.json 兩個配置文件都是可選的, 并且支持當文件被修改時能夠重新加載。
可以在 ASP.NET Core 應(yīng)用中利用這個特性, 實現(xiàn)修改配置文件之后, 不需要重啟應(yīng)用, 自動加載修改過的配置文件, 從而減少系統(tǒng)停機的時間。 實現(xiàn)的步驟如下:
使用配置 API 進行注入
假設(shè)要在程序中注入這樣一個配置類型:
public class WeatherOption {
public string City { get; set; }
public int RefreshInterval { get; set; }
}
在 appsettings.json 中添加的配置如下:
{
"weather": {
"city": "GuangZhou",
"refreshInterval": 120
}
}
在 Startup.cs 的 ConfigureServices 方法中使用配置 API 進行注入, 代碼如下:
public void ConfigureServices(IServiceCollection services) {
services.Configure<WeatherOption>(Configuration.GetSection("weather"));
services.AddControllers();
}
這個步驟很關(guān)鍵, 通過這個配置 API 可以把注入內(nèi)容和配置所在的節(jié)點關(guān)聯(lián)起來。 如果有興趣了解底層實現(xiàn)的話, 可以繼續(xù)查看這個 OptionsConfigurationServiceCollectionExtensions.cs 。
通過這種方式注冊的內(nèi)容, 都是支持當配置文件被修改時, 自動重新加載的。
在控制器 (Controller) 中加載修改過后的配置
控制器 (Controller) 在 ASP.NET Core 應(yīng)用的依賴注入容器中注冊的生命周期是 Scoped , 即每次請求都會創(chuàng)建新的控制器實例。 這樣只需要在控制器的構(gòu)造函數(shù)中注入 IOptionsSnapshot<TOption> 參數(shù)即可, 代碼如下:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase {
private WeatherOption option;
public WeatherForecastController(
IOptionsSnapshot<WeatherOption> options
) {
this.option = options.Value;
}
// GET /weatherforcase/options
[HttpGet("options")]
public ActionResult<WeatherOption> GetOption() {
return options;
}
}
當然, 如果不希望在控制器中使用這個 IOptionsSnapshot 接口類型(會帶來一些對現(xiàn)有代碼重構(gòu)和修改, 還是有一定的風(fēng)險的), 可以在 ConfigureServices 中添加對 WeatherOption 的注入, 代碼如下:
public void ConfigureServices(IServiceCollection services) {
services.Configure<WeatherOption>(Configuration.GetSection("weather"));
// 添加對 WeatherOption 的注入, 生命周期為 Scoped , 這樣每次請求都可以獲取新的配置值。
services.AddScoped(serviceProvider => {
var snapshot = serviceProvider.GetService<IOptionsSnapshot<WeatherOption>>();
return snapshot.Value;
});
services.AddControllers();
}
這樣在控制器中就不需要注入 IOptionsSnapshot<T> 類型了, 最終控制器的代碼如下:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase {
private WeatherOption option;
public WeatherForecastController(
WeatherOption option
) {
this.option = option;
}
// GET /weatherforcase/options
[HttpGet("options")]
public ActionResult<WeatherOption> GetOption() {
return options;
}
}
這樣控制器就無需修改任何代碼即可加載修改過后的新配置。
在中間件 (Middleware) 中加載修改過后的配置
中間件 (Middleware) 在 ASP.NET Core 應(yīng)用的依賴注入容器中注冊的生命周期是 Singleton , 即單例的, 只有在當應(yīng)用啟動時, 根據(jù)中間件創(chuàng)建處理連時創(chuàng)建一次全局實例, 所以只能通過注入 IOptionsMonitor<T> 來監(jiān)聽配置文件的修改情況, 示例代碼如下:
public class TestMiddleware {
private RequestDelegate next;
private WeatherOption option;
public TestMiddleware(
RequestDelegate next,
IOptionsMonitor<WeatherOption> monitor
) {
this.next = next;
option = monitor.CurrentValue;
// moni config change
monitor.OnChange(newValue => {
option = newValue;
});
}
public async Task Invoke(HttpContext context) {
await context.Response.WriteAsync(JsonSerializer.Serialize(option));
}
}
當然, 在中間件的 Task Invoke(HttpContext context) 方法中, 直接獲取 IOptionsSnapshot<T> 也是可以的, 代碼如下:
public async Task Invoke(HttpContext context) {
var snapshot = context.RequestServices.GetService<IOptionsSnapshot<WeatherOption>>();
await context.Response.WriteAsync(JsonSerializer.Serialize(snapshot.Value));
}
但是這么做的話, 似乎就偏離了依賴注入的原則了, 因此不推薦這種做法。
總結(jié)
到此這篇關(guān)于ASP.NET Core中修改配置文件后自動加載新配置的文章就介紹到這了,更多相關(guān)ASP.NET Core自動加載新配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
asp.net下實現(xiàn)URL重寫技術(shù)的代碼
asp.net下實現(xiàn)URL重寫技術(shù)的代碼...2007-10-10
.NET中實現(xiàn)對象數(shù)據(jù)映射示例詳解
這篇文章主要為大家介紹了.NET中實現(xiàn)對象數(shù)據(jù)映射示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

