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

  1. 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();
  1. the appettings is configured in startup as below :

services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

  1. 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;

        }
  1. 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)

Most upvoted comments

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

On Mar 20, 2020, at 1:56 PM, David Fowler notifications@github.com wrote:

cc @HaoK This should work right?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.