runtime: IOptionsSnapshot is not getting the updated values from app settings
I am using .net core 3.1
I am trying the IOptionsSnapshot with strongly typed settings. I am injecting it in a controller as below
- The program class looks like below
public static void Main(string[] args)
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((ctx, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("Config/appsettings.json", optional: false)
.AddJsonFile($"Config/appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true);
})
.UseStartup<Startup>()
.Build();
- the appettings is configured in startup as below :
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
- the controller look like below, injected the IOptionSnapshot in constructor :
private AppSettings _appKeys;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IOptionsSnapshot<AppSettings> appSettings)
{
_logger = logger;
_appKeys = appSettings.Value;
}
- I am using it a method like below for testing , but the output is always the same even after editing the values after running the application
[HttpGet("GetConfigs")]
public List<string> GetConfigs()
{
List<string> configs = new List<string>();
configs.Add(_appKeys.Config1);
configs.Add(_appKeys.Sql.ConnectionString);
return configs;
}
is there anything am missing , or is this a bug ?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 31 (18 by maintainers)
I think it’s just missing reloadOnChange true in the config setup which is what triggers the updates. Otherwise the config would never get updated