mikenlanggio
mikenlanggio

Reputation: 1137

appsetting.json modified but still return old values?

I used the config in Startup.cs file (and in also in other).

app.UseCors(options =>
{
   options.WithOrigins(Configuration["AllowedCors"]).AllowAnyMethod().AllowAnyHeader();
});

I publish my code (to deploy), and sometimes, I have to changes my appsettings.json. But, seem like it's still getting old value. And I always need to restart my app to getting new value work.

Does anyone have anny idea?

Upvotes: 1

Views: 1317

Answers (3)

Hamed Lohi
Hamed Lohi

Reputation: 621

You can use IOptionsSnapshot instead of IOptions for inject settings in the constructor, after set reloadOnChange: true like this answer.

private readonly BranchSettings _settings;

public Constructor(IOptionsSnapshot<BranchSettings> settings)
{
    _settings = settings?.Value;
}

Upvotes: 1

Luka Rakic
Luka Rakic

Reputation: 493

I'm unsure if this is what you are asking, but when you change appsettings.json on a already running deployment, to load new values you have to restart that deployment.
EDIT: Seems like I was mistaken, Ali gave a simple solution without restarting the application.

Upvotes: 0

ali shamekhi
ali shamekhi

Reputation: 99

You should use reloadOnChange: true in your startup file.

public Startup(IWebHostEnvironment env)
    {
        var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        Configuration = builder.Build();
    }

Upvotes: 2

Related Questions