.NET5修改配置不重啟自動(dòng)生效的實(shí)現(xiàn)
.NET Core,.NET5默認(rèn)配置都是只加載一次,修改配置時(shí)都需要重啟才能生效,如何能修改即時(shí)生效呢,下面來演示一遍。
一、設(shè)置配置文件實(shí)時(shí)生效
1.1配置
在Program.cs的CreateHostBuilder()處增加加載配置文件的時(shí)候,reloadOnChange:true。
這樣配置文件修改的時(shí)候,程序就會(huì)監(jiān)聽到文件發(fā)生變化,自動(dòng)重新加載了。
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((context, config) => { config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
1.2驗(yàn)證
appsettings.json文件內(nèi)容如下
{ "TestSetting": "123", "AppOptions": { "UserName": "zhangsan" } }
代碼:
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly IConfiguration _configuration; public HomeController(ILogger<HomeController> logger, IConfiguration configuration) { _logger = logger; _configuration = configuration; } public IActionResult Index() { string Name = _configuration["TestSetting"]; string Name2 = _configuration["AppOptions:UserName"]; ViewBag.Name = Name; ViewBag.Name2 = Name2; return View(); } }
界面顯示:
把配置文件修改為:
{ "TestSetting": "abc", "AppOptions": { "UserName": "zhangsan123" } }
刷新頁面,已經(jīng)發(fā)生變化:
1.3 IOptions方式實(shí)時(shí)生效
新建AppOptions.cs類
/// <summary> /// 配置文件 /// </summary> public class AppOptions { public string UserName { get; set; } }
在Startup.cs處把配置加到Options
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.Configure<AppOptions>(Configuration.GetSection("AppOptions")); }
使用:
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly IConfiguration _configuration; private AppOptions _options; public HomeController(ILogger<HomeController> logger, IConfiguration configuration, IOptionsMonitor<AppOptions> appOptions) { _logger = logger; _configuration = configuration; _options = appOptions.CurrentValue; } public IActionResult Index() { string Name = _configuration["TestSetting"]; string Name2 = _options.UserName; ViewBag.Name = Name; ViewBag.Name2 = Name2; return View(); } }
IOptions有三種方式
//IOptions<T> //站點(diǎn)啟動(dòng)后,獲取到的值永遠(yuǎn)不變 //IOptionsMonitor<T> //站點(diǎn)啟動(dòng)后,如果配置文件有變化會(huì)發(fā)布事件 (加載配置時(shí),reloadOnChange:true 必須為true) //IOptionsSnapshot<T> //站點(diǎn)啟動(dòng)后,每次獲取到的值都是配置文件里的最新值 (加載配置時(shí),reloadOnChange:true 必須為true)
注意:
在 AddSingleton Services中使用 IOptionsMonitor<T>也是無法實(shí)現(xiàn)配置立即生效的,而IOptionsSnapshot<T>啟動(dòng)就會(huì)報(bào)錯(cuò)。
例:
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private UserService _userService; public HomeController(ILogger<HomeController> logger, UserService userService) { _userService = userService; } public IActionResult Index() { string Name2 = _userService.GetName(); ViewBag.Name2 = Name2; return View(); } }
public class UserService { private AppOptions _options; public UserService(IOptionsMonitor<AppOptions> appOptions) { _options = appOptions.CurrentValue; } public string GetName() { var Name = _options.UserName; return Name; } }
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.Configure<AppOptions>(Configuration.GetSection("AppOptions")); services.AddSingleton<UserService>(); }
上面的UserService是單例注入的,通過IOptions的方式是實(shí)現(xiàn)不了配置實(shí)時(shí)刷新的,這種情況只能通過1.2中的 _configuration["AppOptions:UserName"]; 方式獲取才能實(shí)現(xiàn)。
所以,這幾種方式要根據(jù)情景變換使用。
1.4多個(gè)配置文件加載實(shí)時(shí)生效
增加多一個(gè)db配置文件
修改Program.cs處CreateHostBuilder(),也是加載時(shí)加上reloadOnChange:true 就可以了。
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((context, config) => { config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); config.AddJsonFile("Configs/dbsetting.json", optional: true, reloadOnChange: true); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
使用也是一樣的:
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly IConfiguration _configuration; private AppOptions _options; public HomeController(ILogger<HomeController> logger, IConfiguration configuration, IOptionsMonitor<AppOptions> appOptions) { _logger = logger; _configuration = configuration; _options = appOptions.CurrentValue; } public IActionResult Index() { string Name = _configuration["TestSetting"]; string Name2 = _configuration["db:connection1"]; ViewBag.Name = Name; ViewBag.Name2 = Name2; return View(); } }
到此這篇關(guān)于.NET5修改配置不重啟自動(dòng)生效的文章就介紹到這了,更多相關(guān).NET5修改配置不重啟自動(dòng)生效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
未在本地計(jì)算機(jī)上注冊(cè)“microsoft.ACE.oledb.12.0”提供程序報(bào)錯(cuò)的解決辦法
這篇文章主要給大家介紹了關(guān)于未在本地計(jì)算機(jī)上注冊(cè)“microsoft.ACE.oledb.12.0”提供程序報(bào)錯(cuò)的完美解決辦法,需要的朋友可以參考下2019-03-03在.NET中使用Newtonsoft.Json轉(zhuǎn)換,讀取,寫入的方法介紹
Newtonsoft.Json.JsonConvert類是非微軟提供的一個(gè)JSON序列化和反序列的開源免費(fèi)的類庫2012-08-08ASP.NET 站點(diǎn)地圖(sitemap)簡明教程
畢業(yè)設(shè)計(jì)折騰了近一個(gè)月的時(shí)間,也將近完工階段。下個(gè)禮拜六是論文答辯時(shí)間,所以今天晚上就抽空想去弄一下站點(diǎn)地圖。不怕大俠們笑話,我在以前還真沒弄過這些。以前開發(fā)過幾個(gè)項(xiàng)目都是系統(tǒng)類,也就沒怎么涉及了2012-04-04