无法使用IOptionsMonitor来检测ASP.NET Core中的更改 [英] Cannot use IOptionsMonitor to detect changes in ASP.NET Core

查看:496
本文介绍了无法使用IOptionsMonitor来检测ASP.NET Core中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Asp.Net Core应用

I am working on Asp.Net Core app

我想在运行应用程序后更改配置设置

I want to change the configuration settings after running the application

我正在使用IOptionsMonitor,但未检测到更改

I am using IOptionsMonitor, but it is not detecting changes

在Startup.cs->我的Configuration()方法中

In Startup.cs -> Configuration() method I have

services.Configure<Config>(Configuration.GetSection("someConfig"));

在读取这些配置设置的另一个类中,我写了类似的

In a different class where these config settings are read, I wrote something like

var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>>();

但是当我更改配置文件(Json文件)时,未检测到更改,并且someConfig不会更改.

But when I change the configuration file (Json File), the change is not detected, and someConfig does not change.

配置POCO类:

public class Config
{
    public string name {get; set;}
    //More getters and setters
}

services.AddSingleton<ConfigHelpers>;

我正在使用一个单例对象,试图在其中读取配置.如果它不是一个小工具,它就可以正常工作.有没有办法甚至在单例对象中也更改配置?

I am using a singleton object in which I am trying to read the config. It works fine if its not a snigleton. Is there a way to change the config even in a singleton object ?

在ConfigHelpers.cs中

in ConfigHelpers.cs

var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>();

由于在Startup.cs中将其定义为单例,因此不会反映对Config所做的更改.

since it is defined as singleton in Startup.cs, changes made to Config are not reflected.

推荐答案

您是否创建了这样的aprox WebHostBuilder

Did you create your WebHostBuilder like this aprox:

var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("config.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

var AppConfig = config.Get<AppConfig>();

var host = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseUrls("http://*:" + (AppConfig.Port ?? 80))
    .UseKestrel()
    .UseIISIntegration()
    // Clones your config values
    .UseConfiguration(config)
    .UseStartup<Startup>()
    .Build();

然后它将克隆您的值,您将失去实时重新加载功能.

Then it will clone your values and you'll loose the live reload ability.

您必须使用 ConfigureAppConfiguration 并执行以下操作:

You'll have to use ConfigureAppConfiguration and do the following:

var host = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseUrls("http://*:" + (AppConfig.Port ?? 80))
    .UseKestrel()
    .ConfigureAppConfiguration((builder, conf) =>
    {
        conf.SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("config.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables();
    })
    .UseIISIntegration()
    //.UseConfiguration(config)
    .UseStartup<Startup>()
    .Build();

这篇关于无法使用IOptionsMonitor来检测ASP.NET Core中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆