Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dotnet core Console appsettings.json runtime reload

Tags:

c#

.net-core

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

like image 512
user1063108 Avatar asked Apr 08 '26 01:04

user1063108


1 Answers

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.

like image 81
rfmodulator Avatar answered Apr 10 '26 15:04

rfmodulator