欢迎光临
我的个人博客网站

.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();         } }

 

界面显示:

.NET5修改配置不重启自动生效

 

 

 把配置文件修改为:

{   "TestSetting": "abc",   "AppOptions": {     "UserName": "zhangsan123"   } }

刷新页面,已经发生变化:

.NET5修改配置不重启自动生效

 

 

 

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)

 

根据情景使用,这里使用IOptionsMonitor<T>。

1.4多个配置文件加载实时生效

增加多一个db配置文件

.NET5修改配置不重启自动生效

 

 

修改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();         } }

 

赞(0) 打赏
未经允许不得转载:张拓的天空 » .NET5修改配置不重启自动生效
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏