.NET5修改配置不重启自动生效的实现
程序员文章站
2022-06-09 20:26:25
目录一、设置配置文件实时生效1.1配置1.2验证1.3 ioptions方式实时生效1.4多个配置文件加载实时生效.net core,.net5默认配置都是只加载一次,修改配置时都需要重启才能生效,如...
.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修改配置不重启自动生效内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!