Jason N. Gaylord
Jason N. Gaylord

Reputation: 8324

reloadOnChange Not Working in .NET Core 2

I have an ASP.NET Core 2 project running under netcore2. I'm overriding BuildWebHost to add in a new JSON config file like so:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddJsonFile("mysettings.json", optional: false, reloadOnChange: true);
        })
        .UseStartup<Startup>()
        .Build();

However, in an ActionFilter for controller, I'm attempting to grab it like so:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);

    var options = filterContext.HttpContext.RequestServices.GetService(typeof(IOptions<MySettings>));

    /// Do something with the options.
}

The options still appear to be the same options that existed when the app spun up. Any idea why they aren't reloading?

Upvotes: 2

Views: 1321

Answers (1)

Nick Albrecht
Nick Albrecht

Reputation: 16938

Use IOptionsSnapshot<> if you need it to be responsive to changes in the configuration

Upvotes: 3

Related Questions