C# 在Singleton服务中使用IOptionsMonitor在运行时从appsettings重新加载值

C# 在Singleton服务中使用IOptionsMonitor在运行时从appsettings重新加载值,c#,.net,configuration,asp.net-core-2.2,C#,.net,Configuration,Asp.net Core 2.2,我目前正在开发ASP.NET Core 2.2/.NET Framework 4.7.2应用程序(我必须在.NET Framework 4.7.2应用程序中安装.NET Core 2.2作为框架)。我需要从单例服务中的appsettings.json文件中检索值(这些值可以通过手动更改appsettings.json文件在运行时更改)。这个很好用。但是,所需的reloadOnChange不起作用 当我在运行时更改appsettings.json文件中的值,然后触发对服务逻辑的新请求时,不会检索新

我目前正在开发ASP.NET Core 2.2/.NET Framework 4.7.2应用程序(我必须在.NET Framework 4.7.2应用程序中安装.NET Core 2.2作为框架)。我需要从单例服务中的
appsettings.json
文件中检索值(这些值可以通过手动更改appsettings.json文件在运行时更改)。这个很好用。但是,所需的
reloadOnChange
不起作用

当我在运行时更改appsettings.json文件中的值,然后触发对服务逻辑的新请求时,不会检索新值。我已经尝试将我的单例服务作为作用域注入,不幸的是徒劳——同样的结果,没有更新的配置

我真的不知道为什么appsettings.json中更新的值在运行时从未出现在我的服务类中

我的
CreateWebHostBuilder
方法如下所示:

public IWebHostBuilder CreateWebHostBuilder()
{
    var config = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile(configFilePath, optional: false, reloadOnChange: true)
       .Build();

       return WebHost.CreateDefaultBuilder()
            .UseStartup<Startup>()
            .UseConfiguration(config);
 }
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public static IConfiguration Configuration { get; private set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<AppSettings>(Configuration);

        services.AddSingleton<IFruitService, FruitService>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    { }
}
public class FruitService : IFruitService
{
    private readonly IOptionsSnapshot<AppSettings> appSettings;

    public PomService(IOptionsSnapshot<AppSettings> appSettings)
    {
        this.appSettings = appSettings;

    }

    public async Task<FruitMix> StartFruitMix(List<Fruit> fruits)
    {
        // here I would need the changed values from the appsettings.json,
        // but always the same, no change in any request...
        var a = appSettings.Value.Test;
        var b = appSettings.Value;
        var c = appSettings.Get("");

    }
}
{
  "test": "1234" // during runtime when I manually change 1234 to sadf or 567 nothing happens in my service class on a new request (nor in singleton neither in scoped mode... I cannot retrieve the new value in my service class.)
}
我的
水果服务
如下所示:

public IWebHostBuilder CreateWebHostBuilder()
{
    var config = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile(configFilePath, optional: false, reloadOnChange: true)
       .Build();

       return WebHost.CreateDefaultBuilder()
            .UseStartup<Startup>()
            .UseConfiguration(config);
 }
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public static IConfiguration Configuration { get; private set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<AppSettings>(Configuration);

        services.AddSingleton<IFruitService, FruitService>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    { }
}
public class FruitService : IFruitService
{
    private readonly IOptionsSnapshot<AppSettings> appSettings;

    public PomService(IOptionsSnapshot<AppSettings> appSettings)
    {
        this.appSettings = appSettings;

    }

    public async Task<FruitMix> StartFruitMix(List<Fruit> fruits)
    {
        // here I would need the changed values from the appsettings.json,
        // but always the same, no change in any request...
        var a = appSettings.Value.Test;
        var b = appSettings.Value;
        var c = appSettings.Get("");

    }
}
{
  "test": "1234" // during runtime when I manually change 1234 to sadf or 567 nothing happens in my service class on a new request (nor in singleton neither in scoped mode... I cannot retrieve the new value in my service class.)
}
我的
appsettings.json
如下所示:

public IWebHostBuilder CreateWebHostBuilder()
{
    var config = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile(configFilePath, optional: false, reloadOnChange: true)
       .Build();

       return WebHost.CreateDefaultBuilder()
            .UseStartup<Startup>()
            .UseConfiguration(config);
 }
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public static IConfiguration Configuration { get; private set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<AppSettings>(Configuration);

        services.AddSingleton<IFruitService, FruitService>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    { }
}
public class FruitService : IFruitService
{
    private readonly IOptionsSnapshot<AppSettings> appSettings;

    public PomService(IOptionsSnapshot<AppSettings> appSettings)
    {
        this.appSettings = appSettings;

    }

    public async Task<FruitMix> StartFruitMix(List<Fruit> fruits)
    {
        // here I would need the changed values from the appsettings.json,
        // but always the same, no change in any request...
        var a = appSettings.Value.Test;
        var b = appSettings.Value;
        var c = appSettings.Get("");

    }
}
{
  "test": "1234" // during runtime when I manually change 1234 to sadf or 567 nothing happens in my service class on a new request (nor in singleton neither in scoped mode... I cannot retrieve the new value in my service class.)
}
您知道如何检索服务类中更改的值吗


非常感谢

选项,即使启用了重新加载,在请求生命周期内也不会更改。但是,听起来您实际上提出了一个新的请求,并且发现选项仍然没有更改

我个人没有遇到过需要专门使用
IOptionsMonitor
的情况。但是,我知道它在内部使用缓存,并具有手动使所述缓存中的选项无效的能力。有可能它实际上不会在更改时自动重新加载-对此不确定


无论如何,使用
IOptionsSnapshot
更为典型。这仅仅是为了每个请求重新加载选项,所以它似乎满足了您的需要。
IOptionsMonitor
的唯一好处似乎是它能够实际监视更改并通知回调。再说一次,我还没有充分地使用它来判断您是否只是做错了什么,但我认为您实际上并不需要它。

谢谢Chris Pratt,通常IOptionsSnapshot在纯.NET Core 2环境中应该可以正常工作。在我的例子中,我使用.NETCore2作为我的.NETFramework项目中的一个框架。因此,我想问题出在哪里了。我发现,我需要使用ConfigureAppConfiguration,它就像一个charm.Jeez。对不起,我完全错过了。