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
CodePudding user response:
try this
public IConfiguration GetAppssetingsConfig()
{
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}
CodePudding user response:
The configuration is monitoring (via reloadOnChange: true
) the appsettings.json file in the current working directory (via Directory.GetCurrentDirectory()
).
This is the build directory, if debugging.
Edits to the project's appsettings.json file (the "master" copy) won't be reflected while debugging. Instead, edit the copy in the build directory.