何时真正读取来自app.config的设置? [英] When are settings from app.config actually read?

查看:100
本文介绍了何时真正读取来自app.config的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时真正从应用程序读取app.config中的设置?

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

假设我有Windows服务和一些应用程序设置.在代码中,我有一种使用某些设置的方法.方法在每次迭代中都会被调用,而不是一直都被调用一次.如果我通过配置文件更改设置值,是否应该重新启动服务以使其在内部刷新",还是下次可以接受而无需我的任何干预?

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?

推荐答案

您需要致电

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);
    }
}

我的app.config包含:

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>

启动应用程序后,打开构建文件夹中的app.config,并更改appSetting"myKey"的值.您将看到新值打印到控制台.

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.

这篇关于何时真正读取来自app.config的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆