Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change in application configuration without application restart

I have following issue: I'm introducing new feature into application (run as Windows Service) and I would like to have that new feature controlled (on/off) using some kind of configuration file entry (myKey). I can store config entry in app.config but if I want to change it from on->off or otherwise it would require to restart Windows Service, and I want to avoid it. I want my application to run and pick up changes in configuration.

The question is: is there a build in mechanism in .NET that addresses that problem ? I guess I could create my own config file, then use FileSystemWatcher etc... But perhaps .NET allows to use external config files and will reload value ??

ConfigurationManager.AppSettings["myKey"]

Thanks, Pawel

EDIT 1: Thanks for replies. However I wrote following snippet and it doesn't work (I tried creating appSettingSection in both places: before and inside loop):

static void Main(string[] args)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    // AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
    for (int i = 0; i < 10; i++)
    {
        ConfigurationManager.RefreshSection("appSettings");
        AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
        string myConfigData = appSettingSection.Settings["myConfigData"].Value; // still the same value, doesn't get updated
        Console.WriteLine();
        Console.WriteLine("Using GetSection(string).");
        Console.WriteLine("AppSettings section:");
        Console.WriteLine(
          appSettingSection.SectionInformation.GetRawXml()); // also XML is still the same
        Console.ReadLine();
    }
}

I manually edit config file when application stops on Console.ReadLine().

like image 405
dragonfly Avatar asked Apr 22 '11 06:04

dragonfly


People also ask

Which file is the place where we can change the configuration of the app?

xml file is the place where we can change the configuration of the app.

What does application configuration mean?

An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly. This is called per-application configuration.

When should I use app config?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


1 Answers

Once the original app.config file is loaded, it's values are cached so as you know, you'll have to restart the app. The way around this is to create a new config object and read the keys manually like this:

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string myConfigData = appConfig.AppSettings.Settings["myConfigData"].Value;
like image 71
Teoman Soygul Avatar answered Oct 02 '22 19:10

Teoman Soygul