更改 tets 项目的 app.config 文件中的配置值

发布于 2024-12-11 23:54:05 字数 1629 浏览 0 评论 0原文

我正在尝试测试一些依赖于配置值的功能(如果 Settings["foo"] = true 则返回 5,否则返回 -1)。

所以我想做的是在运行时更改配置值。
我的配置文件看起来像这样(简化):

    <configSections>
      <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">      
        <section name="DomainSettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
      </sectionGroup>
    </configSections>

<applicationSettings>
<DomainSettings>
    <setting name="foo" serializeAs="String">
      <value>false</value>
    </setting>
</ICTS.SmartQueue.Domain.DomainSettings>
</applicationSettings>

并且我正在执行以下操作:

//get config file
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//get relevant section
var section = (ClientSettingsSection)config.GetSection("applicationSettings/DomainSettings");
//get element from section
var element = section.Settings.Get("Foo"); 
//change its value and save it
element.Value.ValueXml.InnerText = true.ToString();
config.Save(System.Configuration.ConfigurationSaveMode.Modified, true);
//force refresh
ConfigurationManager.RefreshSection("applicationSettings/DomainSettings");

当我查看“Out”目录中的测试配置文件(MyTests.DLL.config)时,我可以看到该值实际上已更改。< br> 但是,DomainSettings.Default.Foo 的计算结果仍然为“false”。

有什么想法吗?

i'm trying to test some functionality which is dependant on a configuration value (if Settings["foo"] = true than return 5, otherwise- return -1).

So what I'm trying to do is to change the configuration value at runtime.
my config file looks like so (simplified):

    <configSections>
      <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">      
        <section name="DomainSettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
      </sectionGroup>
    </configSections>

<applicationSettings>
<DomainSettings>
    <setting name="foo" serializeAs="String">
      <value>false</value>
    </setting>
</ICTS.SmartQueue.Domain.DomainSettings>
</applicationSettings>

and I'm doing the following:

//get config file
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//get relevant section
var section = (ClientSettingsSection)config.GetSection("applicationSettings/DomainSettings");
//get element from section
var element = section.Settings.Get("Foo"); 
//change its value and save it
element.Value.ValueXml.InnerText = true.ToString();
config.Save(System.Configuration.ConfigurationSaveMode.Modified, true);
//force refresh
ConfigurationManager.RefreshSection("applicationSettings/DomainSettings");

I can see that the value is actually changed when I look at the test's config file in the 'Out' directory (MyTests.DLL.config).
However, DomainSettings.Default.Foo still evaluates to 'false'.

any ideas?

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

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

发布评论

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

评论(2

贪了杯 2024-12-18 23:54:05

配置文件已缓存。换句话说,仅仅因为您使用新值修改了配置文件,它就不会重新加载,直到应用程序重新加载。然后,您将看到新值发生变化。但是,当您在代码中引用配置文件时,它不会读取该文件,而是读取缓存的配置。在您的情况下,这没有用您的新值更新。

The config file is cached. In other words, just because you modify the config file with a new value, it will not reload until the application reloads. Then, you will see the new value changed. But when you reference the config file in your code, it doesn't read the file, it reads the config that is cached. And in your case, that is not updated with your new value.

箜明 2024-12-18 23:54:05

这是因为您可以在运行时更改配置文件,但只有重新启动应用程序后,更改才会生效。 ASP.NET 则不同,它会在 Web.config 文件更改后立即获取更改。

That's because you can change the config file at runtime but the changes won't be picked up until you restart the app. ASP.NET is different, it will pick up the changes immediately after the Web.config file changes.

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