'C# Dotnet core Console appsettings.json runtime reload
I have a need where I may change appsettings.json while my Console app is running. The code I am using to load appsettings.json is only loads the appsettings.json at startup and it never refreshes once the app is running. Can some one help me figure this out pls?
public IConfigurationRoot GetAppssetingsConfig()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
configuration.Reload();
return configuration;
}
What I am expecting is that each time when I call the above function, it reads the appsettings.json at that time, however this is not happening. Thanks for help
Solution 1:[1]
try this
public IConfiguration GetAppssetingsConfig()
{
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Serge |
