monty
monty

Reputation: 8755

How does reloadOnChange of Microsoft.Extensions.Configuration work for appsettings.json

In two projects (a .NET Core Web API and a .NET Core WindowsService) I am using appsettings.json for the configuration.

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

In both I have the reloadOnChange set to true and using it as injected IOptions via dependency injection. Within the web api into the controller classes and within the service into the classes that use the settings.

Unfortunatly I experience that the values do not change when the appsettings.json changes.

On the web api I created a controller to just return a string value from the config and this stays the same as on startup.

So my questions:

  1. Anyone knows if that should work out of the box (at least in the web api)?
  2. Anything I have to do that it works?

Upvotes: 27

Views: 18438

Answers (1)

Boggin
Boggin

Reputation: 3416

Assuming you are using .net-core 1.1 (because reloadOnChange is only supported in ASP.NET Core 1.1 and higher) it's actually IOptionsSnapshot you want (cf. Configuration in ASP.NET Core - IOptionsSnapshot) rather than just IOptions.

Upvotes: 12

Related Questions