Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are settings from app.config actually read?

When are settings from app.config actually read by application?

Suppose I have a windows service and some app settings for it. In code I have a method where some setting is used. Method is being called in every iteration, not just once during all the time. If I change the setting value through the configuration file should I restart the service for it to be "refreshed" inside or will it be accepted the next time without any interaction from my side?

like image 299
26071986 Avatar asked Feb 08 '12 17:02

26071986


People also ask

How does app config work?

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.

How do you read in configuration settings from the application config file?

Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.

Does app config get compiled?

Now you might be wondering what happens behind the scenes. Well, when you compile your application, the compiler actually copies the app. config file to the output folder, but gives it another name: When you start your application (ConsoleApp1.exe in our example), the matching config file will be loaded too.

What is app setting in web config?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.


1 Answers

You need to call ConfigurationManager.RefreshSection method to get the latest values read directly from disk. Here's a simple way to test and provide answer to your question:

static void Main(string[] args)
{
    while (true)
    {
        // There is no need to restart you application to get latest values.
        // Calling this method forces the reading of the setting directly from the config.
        ConfigurationManager.RefreshSection("appSettings");
        Console.WriteLine(ConfigurationManager.AppSettings["myKey"]);

        // Or if you're using the Settings class.
        Properties.Settings.Default.Reload();
        Console.WriteLine(Properties.Settings.Default.MyTestSetting);

        // Sleep to have time to change the setting and verify.
        Thread.Sleep(10000);
    }
}

My app.config containing:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="ConsoleApplication2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="myKey" value="Original Value"/>
  </appSettings>
  <userSettings>
    <ConsoleApplication2.Properties.Settings>
      <setting name="MyTestSetting" serializeAs="String">
        <value>Original Value</value>
      </setting>
    </ConsoleApplication2.Properties.Settings>
  </userSettings>
</configuration>

After you start the application, open the app.config within the build folder, and change the value of the appSetting "myKey". You'll see the new value printed out to the console.

To answer the question, yes they are cached on the first time they are each read I think, and to force the read straight from the disk, you need to refresh the section.

like image 73
mservidio Avatar answered Oct 18 '22 13:10

mservidio