带有IOptionsSnapshot的ASP.NET Core配置reloadOnChange仍然没有响应 [英] ASP.NET Core configuration reloadOnChange with IOptionsSnapshot still not responsive

查看:232
本文介绍了带有IOptionsSnapshot的ASP.NET Core配置reloadOnChange仍然没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core 2.0,并且在Main方法中具有这样的配置代码:

I'm using ASP.NET Core 2.0 and I have configuration code like this in the Main method:

public static void Main(string[] args)
{
    var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment ?? "Production"}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .AddCommandLine(args)
        .Build();
}

我将reloadOnChange设置为true,并且在我的控制器中,我正在使用IOptionsSnapshot;

I have the reloadOnChange set to true, and in my controller I am using IOptionsSnapshot;

public HomeController(ILogger<HomeController> logger, IOptionsSnapshot<AppSettings> options)

但是,当我修改appsettings.json中的值时,每次都必须重新启动我的应用程序,否则仅通过刷新浏览器就无法获取更改.我究竟做错了什么?

But when I modify the values in my appsettings.json, I have to restart my app every time or the changes are not being picked up just by refreshing the browser. What am I doing wrong?

我试图同时通过控制台和IIS Express运行该应用程序;我也尝试过IOptionsMonitor,同样的事情. IOptionsMonitorIOptionsSnapshot有什么区别?

I've tried to run the app both with console and IIS Express; I've also tried IOptionsMonitor, same thing. What is the difference between IOptionsMonitor and IOptionsSnapshot?

推荐答案

As mentioned in the documentation, just enabling reloadOnChange and then injecting IOptionsSnapshot<T> instead of IOptions<T> will be enough. That requires you to have properly configured that type T though. Usually a configuration registration will look like this:

services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

但是,仔细查看您的代码,似乎您没有使用新的ASP.NET Core 2.0配置程序的方式.现在,该配置是依赖项注入的一部分,因此您将使用ConfigureAppConfiguration将其设置为WebHostBuilder的一部分.例如,可能看起来像这样:

However, looking closer at your code, it does not seem that you are using the new ASP.NET Core 2.0 way of configuring your program. The configuration is now part of dependency injection, so you will set it up as part of the WebHostBuilder, using ConfigureAppConfiguration. That could for example look like this:

public static IWebHost BuildWebHost()
    => new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration((builderContext, config) =>
        {
            IHostingEnvironment env = builderContext.HostingEnvironment;

            config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
        })
        .UseStartup<Startup>()
        .Build();

如果您使用的默认构建器使用WebHost.CreateDefaultBuilder(),那么您甚至不需要这样做,因为配置会像激活reloadOnChange一样自动设置.

If you are using the default builder using WebHost.CreateDefaultBuilder(), then you don’t even need to do this, as the configuration is then automatically set up like that with reloadOnChange activated.

IOptionsSnapshotIOptionsMonitor之间的区别在于,IOptionsSnapshot只会在构建IOptionsSnapshot<T>对象时为您提供选项的快照.

The difference between IOptionsSnapshot and IOptionsMonitor is that the IOptionsSnapshot will just give you a snapshot of the options at the time the IOptionsSnapshot<T> object is being constructed.

这就是用法与IOptions<T>完全相同的原因:您将其注入到构造函数中,然后将options.Value存储在实例中以供以后访问选项.在这一点上,该对象是固定的,将永远不会改变.只是IOptionsSnapshot<T>被注册为作用域的依赖项,而不是像IOptions<T>这样的单例依赖项,因此它有机会获得每个请求的当前配置值,而不仅仅是一次.

That’s why the usage is exactly the same as with IOptions<T>: You inject it in the constructor and then store the options.Value in the instance to access the options later. At that point, that object is fixed and will never change. It’s just that the IOptionsSnapshot<T> is registered as a scoped dependency instead of a singleton dependency like IOptions<T>, so it gets the chance to get the current configuration values on every request instead of just once.

IOptionsMonitor<T>是单例服务,允许您在任何给定时间检索当前配置值.因此,它对于需要在需要时获取当前配置的单例服务特别有用.另外,选项监视器提供了推送机制,可通过配置源通知配置更改.这样,您可以显式处理配置更改.

The IOptionsMonitor<T> however is a singleton service that allows you to retrieve the current configuration value at any given time. So it is especially useful for singleton services that need to get the current configuration whenever they need it. In addition, the options monitor offers a push mechanism to get notified of configuration changes by the configuration sources. That way, you can explicitly handle configuration changes.

选项快照旨在用于临时或范围内的依赖项,因此大多数时间使用这些快照就可以了.仅在极少数情况下,当您必须使用需要具有最新配置的单例服务时,才需要使用选项监视器.在这种情况下,请注意仅从快照切换到监视器是不够的.通常,您将必须以某种方式处理更改的配置(例如,清理状态,清除缓存等).因此,您应该始终考虑是否真的需要为一切重新加载配置,或者只是重新启动应用程序不是可行的选择.

Options snapshots are designed to be used for transient or scoped dependencies, so you will be fine just using those most of the time. Only in rare occasions when you have to use a singleton service that also needs to have the most updated configuration, you should have the need to use an options monitor. In those cases, note that just switching from snapshots to a monitor will not be enough. Usually, you will have to handle changed configuration in some way (for example clean up state, clear caches etc.). So you should always think about whether you actually need reloadable configuration for everything or if just restarting the application isn’t a viable alternative.

这篇关于带有IOptionsSnapshot的ASP.NET Core配置reloadOnChange仍然没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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