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:
The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.
To define the connection strings in appsettings. json it is important to specify it in the right section of the JSON structure. Now we can read it in our code by calling the GetConnectionString method in the Microsoft. Extensions.
CreateDefaultBuilder()Initializes a new instance of the WebHostBuilder class with pre-configured defaults.
Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With