This article explains a curious workaround that I figured out. In case you need a way to force appsettings s changes to be reflected in some libraries you need in your application, even if they don’t support any configuration after application startup.
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.
So, the solution is to modify the actual appsettings 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?
- Modify application startup
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 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’ll obviously only work if you’re using ConfigurationBuilder to load configuration – maybe in your Program.cs, Startup.cs or in a ServiceFactory class.
- Modify your appsettings 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!
As usual, your Regular Expression might require some dark magic to get it right. But I trust you can figure that out!
- Make sure the changes have been stored properly
Now – start your application and make sure the configuration get’s 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:
And there we are. Hope it’s useful!
- How to fix NU3028 and NU3037 errors in Azure DevOps builds? - September 20, 2022
- How to kill the process causing the “MSB3021 Unable to copy file — The process cannot access the file because it is being used by another process.” error? - September 13, 2022
- How to replace and reload application settings changes in .NET? - September 6, 2022