ASP.NETOnChange kernel configuration update with IOptionsSnapshot still not responding

I am using ASP.NET Core 2.0, and I have this configuration code in the method Main:

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

My reloadOnChange is set to true, and in my controller I useIOptionsSnapshot

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

But when I change the values ​​in mine appsettings.json, I have to restart the application every time or the changes are not selected, just updating the browser. What am I doing wrong? I tried to run the application with both the console and IIS Express; I also tried IOptionsMonitorthe same thing. Btw., What is the difference between IOptionsMonitorand IOptionsSnapshot?

+4
source share
1 answer

, reloadOnChange, IOptionsSnapshot<T> IOptions<T>. , T. :

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

, , , ASP.NET Core 2.0 . , WebHostBuilder, ConfigureAppConfiguration. :

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.


IOptionsSnapshot IOptionsMonitor , IOptionsSnapshot IOptionsSnapshot<T>.

, IOptions<T>: , options.Value , . . , IOptionsSnapshot<T> , singleton, IOptions<T>, , .

IOptionsMonitor<T> - , . singleton, , . , push . , .

, . , , , . , . - (, , ..). , , .

+8

Source: https://habr.com/ru/post/1686900/


All Articles