何时实际读取 app.config 中的设置?

发布于 2025-01-03 12:52:49 字数 166 浏览 1 评论 0原文

应用程序何时实际读取 app.config 中的设置?

假设我有一个 Windows 服务和一些应用程序设置。在代码中,我有一种使用某些设置的方法。方法在每次迭代中都会被调用,而不仅仅是一次。如果我通过配置文件更改设置值,我应该重新启动服务以使其在内部“刷新”,还是下次在没有我这边交互的情况下接受它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2025-01-10 12:52:49

您需要调用 ConfigurationManager.RefreshSection 方法来获取直接从磁盘读取的最新值。这是测试并回答您的问题的简单方法:

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 包含:

<?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”的值。您将看到新值打印到控制台。

为了回答这个问题,是的,我认为它们在第一次读取时被缓存,并且要强制直接从磁盘读取,您需要刷新该部分。

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.

长发绾君心 2025-01-10 12:52:49

当您通过配置管理器 (ConfigurationManager.GetSection("x/y");) 加载它时或当您尝试访问属性时。

这里有一个轻微的灰色区域,因为当您通过配置管理器获取配置时:

var config = (MyConfigSection)ConfigurationManager.GetSection("MyConfigSection");

如果您在配置文件顶部的configurationSections元素中提供了配置节类型,您将获得一个配置对象。如果您实际上没有提供实际的配置,您仍然会得到一个对象。

但是,如果您有未设置的必填字段,则在调用该属性之前它不会抛出异常。我在尝试对我的自定义配置部分进行单元测试时已经解决了这个问题。

Either when you load it up via the configuration manager (ConfigurationManager.GetSection("x/y");) or when you try to access the properties.

There is a slight grey area here because when you get the configuration out via the config manager:

var config = (MyConfigSection)ConfigurationManager.GetSection("MyConfigSection");

You get a configuration object back if you have provided the configuration section type in the configurationSections element at the top of the config file. If you do not actually provide the actual config you will still get an object back.

However if you have a required field that is not set it will not throw an exception till you call the property. I have worked this out whilst trying to unit test my custom configuration sections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文