How reloadOnChange from Microsoft.Extensions.Configuration works for appsettings.json

In two projects (.NET Core Web API and .NET Core WindowsService) I use appsettings.json for configuration.

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

In both cases, I set reloadOnChange to true and use it as the injected IOptions through dependency injection. Inside the web api in the controller classes and inside the service into classes that use settings.

Unfortunately, I feel that the values ​​do not change when appsettings.json changes.

In the web api, I created a controller to simply return the string value from config, and this will remain the same as when I started it.

So my questions are:

  • Does anyone know if this should work out of the box (at least in the web api)?
  • Anything I have to do for it to work?
+5
source share
1 answer

Assuming you are using .net-core 1.1 (since reloadOnChange is only supported in ASP.NET Core 1.1 and later), you actually want IOptionsSnapshot (see Configuration in ASP.NET Core - IOptionsSnapshot ), not just IOptions .

+3
source

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


All Articles