Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reloading .NET config file

I need to reload the configuration file after modifying it. How this can be done using appdomains? A code sample would be useful.

like image 812
Markus Avatar asked Mar 17 '10 19:03

Markus


People also ask

How do I reload a config file?

To reload Config, select the loaded configurations you want to reload and click Reload. You can only reload a configuration that has the status of Loaded. To refresh Configs, click Refresh. Information for all of the configs in the table is redisplayed.

Where is .NET configuration file?

There is a global configuration file for all sites in a given machine which is called Machine. config. This file is typically found in the C:\WINDOWS\Microsoft.NET\Framework\v2. 0.50727\CONFIG directory.

How can add config file in asp net?

config file name extension is protected from being downloaded by ASP.NET. Click Add to create the file and open it for editing. The file contains the code shown in the "Example" section later in this topic, with a few initial defaults. Your application inherits all configuration settings from the Machine.


1 Answers

Let's say you have the following config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="test" value="1" />
  </appSettings>
</configuration>

Let's try the naive approach first. The following application will try to grab the value of the appSetting named test once per second, and print its value:

static void Main(string[] args)
{
    while(true)
    {
        Console.WriteLine(ConfigurationManager.AppSettings["test"]);
        Thread.Sleep(1000);
    }
}

But alas! While this is running, you'll notice it keeps printing 1, and doesn't pick up any changes.

If you update your code to the following, it will fix this issue, and it will pick up changes whenever you change it:

static void Main(string[] args)
{
    while(true)
    {
        ConfigurationManager.RefreshSection("appSettings");
        Console.WriteLine(ConfigurationManager.AppSettings["test"]);
        Thread.Sleep(1000);
    }
}
like image 105
Jay Sullivan Avatar answered Sep 21 '22 12:09

Jay Sullivan