i was digging to find out the solution but didn't manage to find it, i bet that someone has encountered this problem, so what is the problem?.
For test i have created simple console application (solution will be used in asp.net core web api).
I have TestSetting.json configuration file with 'Copy Always' setuped.
{
"setting1" : "value1"
}
And Simple code
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
IConfigurationRoot configuration = configurationBuilder.AddJsonFile("TestSettings.json",false, reloadOnChange: true).Build();
Console.WriteLine(configuration.GetSection("setting1").Value); // Output: value1
//Change configuration manually in file while console is waiting
Console.ReadKey();
//Changed manually in file Value appears
Console.WriteLine(configuration.GetSection("setting1").Value); // Output: Whatever you have setuped
Console.ReadKey();
configuration.GetSection("setting1").Value = "changed from code";
//Changed in code value appear
Console.WriteLine(configuration.GetSection("setting1").Value); // Output: changed from code
I have 2 requirements, i want to make it possible to change value in json configuration file manually while application is running and application will see updated value during next get of setting Section and it is working.
Second requirement is that, i want to preserve some information, to be accurate a last execution time of task which should be executed once per setuped period ex. once a day, so some loop check last execution time value and determine if operation has to be executed. Someone would ask that what i have will work, but i need also to cover scenario when operation has been executed and application has been restarted (server error, user restart etc), and i need to save this information in a way which will allow me to read it after app startup.
Reading code sample we can see that after changing setting1 in code we see that this section has been changed while trying to output it to console.
configuration.GetSection("setting1").Value = "changed from code";
//Changed in code value appear
Console.WriteLine(configuration.GetSection("setting1").Value); // Output: changed from code
Here comes the question :). Is it possible that this settings section change will also affect actual value in json file? I don't want to manually change this file by some stream writers or whatever.
Actual result is that: after changing value in code, the new value is getable in runtime but when you will go to debug binaries you will se that value1 in TestSettings.json file hasnt been changed.