Have you tried turning it off and on again?

How to replace and reload application settings changes in .NET?

This post was most recently updated on November 28th, 2022.

2 min read.

This article explains a curious workaround that I figured out. In case you need a way to force app setting changes to be reflected in some libraries you need in your application, even if they don’t support any configuration after application startup.

Problem

Normally, a .NET application loads the configuration – environment variables, appsettings.json files, wherever your secrets come from, and all the other good stuff – when it’s started, and it’s good with that. If you’re being fancy, you can also use IOptionsSnapshot – which reloads automatically – but unfortunately, I was using a library that would bind to JSON configuration in appsettings.json (or appsettings.local.json) file, and I couldn’t really modify it. However, I had to configure the library with values I’d only get after starting a scheduled task – and the library didn’t expose any configuration values. The only way to modify the values was to change them in the configuration JSON file before binding.

Solution

So, the solution is to modify the actual app settings files and then force your application to reload them. Sounds straightforward enough, right?

One way to do that is to use ConfigurationBuilder’s AddJsonFile, which has a signature like below:

public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddJsonFile (this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange);

The important part being “bool reloadOnChange“!

I’m sure other ways exist, but this is the way I went with, so it’s the one I’m documenting :)

Time needed: 10 minutes

How to replace and reload application settings changes in .NET?

  1. Modify application startup

    The first step is to modify your application startup to make sure changes to appsettings.json file(s) are reloaded to your app. One way to do this is to add a third parameter in ConfigurationBuilder’s AddJsonFile() – something like shown below:

    IConfiguration config = new ConfigurationBuilder() .AddJsonFile(“appsettings.json”, false, true) .AddJsonFile(“appsettings.local.json”, true, true).Build();

    This obviously only works if you’re using ConfigurationBuilder to load configuration – maybe in your Program.cs, Startup.cs or in a ServiceFactory class.

  2. Modify your app settings file

    Now you’ll write some code to modify the actual file on disk. Something like this below:

    // First we figure out the path for the appsettings file:
    var appSettingsPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "appsettings.json");

    // Now let's read the file:
    var json = File.ReadAllText(appSettingsPath);

    // Now let's replace the contents of a JSON configuration node called "Key":
    Regex regex = new Regex("\"Key\":\s*\d+,");
    json = regex.Replace(json, "\"Key\":\"NewValue\"",");

    // And then let's write it back!
    File.WriteAllText(appSettingsPath, json);


    As usual, your Regular Expression might require some dark magic to get it right. But I trust you can figure that out!

  3. Make sure the changes have been stored properly

    Now – start your application and make sure the configuration gets updated as expected! As soon as you’ve updated the file, you should see something like this in the console or logs (depending on your application type):
    New configuration loaded for source
    New configuration loaded for source


    Or you know, something like this:
    New configuration loaded for source


And there we are. Hope it’s useful!

References

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments