.NET5修改配置不重启自动生效的实现
目录
- 一、设置配置文件实时生效
- 1.1配置
- 1.2验证
- 1.3 IOptions方式实时生效
- 1.4多个配置文件加载实时生效
.NET Core,.NET5默认配置都是只加载一次,修改配置时都需要重启才能生效,如何能修改即时生效呢,下面来演示一遍。
一、设置配置文件实时生效
1.1配置
在Program.cs的CreateHostBuilder()处增加加载配置文件的时候,reloadOnChange:true。
这样配置文件修改的时候,程序就会监听到文件发生变化,自动重新加载了。
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验证
appsettings.json文件内容如下
{ "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" } }
刷新页面,已经发生变化:
1.3 IOptions方式实时生效
新建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> //站点启动后,获取到的值永远不变 //IOptionsMonitor<T> //站点启动后,如果配置文件有变化会发布事件 (加载配置时,reloadOnChange:true 必须为true) //IOptionsSnapshot<T> //站点启动后,每次获取到的值都是配置文件里的最新值 (加载配置时,reloadOnChange:true 必须为true)
注意:
在 AddSingleton Services中使用 IOptionsMonitor<T>也是无法实现配置立即生效的,而IOptionsSnapshot<T>启动就会报错。
例:
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的方式是实现不了配置实时刷新的,这种情况只能通过1.2中的 _configuration["AppOptions:UserName"]; 方式获取才能实现。
所以,这几种方式要根据情景变换使用。
1.4多个配置文件加载实时生效
增加多一个db配置文件
修改Program.cs处CreateHostBuilder(),也是加载时加上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(); } }
到此这篇关于.NET5修改配置不重启自动生效的文章就介绍到这了,更多相关.NET5修改配置不重启自动生效内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)