.NET 配置节设计器 - 我的集合在哪里?

发布于 2024-07-16 07:09:31 字数 1191 浏览 9 评论 0原文

我正在使用 .NET 的 配置节设计器 构建一个简单的 ConfigurationElementCollection。 看起来一切都构建得很好,并且代码是自动生成的,因为子元素通过 Intellisense 可见。

不幸的是,即使我在配置部分向集合中添加了元素,它们在运行时并不存在; 它显示集合的 Length 属性为 0 - 即集合是空的,尽管如此,正如您在我的示例 XML 中看到的,我已经清楚地将它们放在那里:

示例配置

<logParserSettings xmlns="LogParser">
  <domainControllers>
    <domainController ID="0" name="Local" serverType="Local" enabled="true"/>
    <domainController ID="1" name="DC1" serverType="WindowsServer2003" enabled="false" />
    <domainController ID="2" name="DC2" serverType="WindowsServer2008" enabled="false" />
  </domainControllers>
</logParserSettings>

其他

  • 在设计器中,我所有的有就是您在这里看到的,一个 ConfigurationSection、一个 ConfigurationElementCollection 和一个 ConfigurationElement -
  • 我 没有其他配置元素/部分/组在运行时不添加或删除任何内容
  • 我尝试过生成和不生成 Singleton 属性,但单例实例和设置类的实例似乎都不起作用
  • 对于 ID 属性, < code>Is Key 属性设置为 true,并且所有其他属性都标记为 true Is required

有人遇到过这种情况吗? 如果是这样,我需要做出哪些改变才能使其按预期工作?

I am using the Configuration Section Designer for .NET to build a simple ConfigurationElementCollection. It appears that everything builds just fine and the code is automatically generated since the sub-elements are visible with Intellisense.

Unfortunately, even though I have added elements to the collection in the configuration section, they do not exist at run-time; it shows the collection's Length property to be 0 - i.e. the collection is empty even though, as you can see in my example XML, I have clearly put them there:

Example configuration

<logParserSettings xmlns="LogParser">
  <domainControllers>
    <domainController ID="0" name="Local" serverType="Local" enabled="true"/>
    <domainController ID="1" name="DC1" serverType="WindowsServer2003" enabled="false" />
    <domainController ID="2" name="DC2" serverType="WindowsServer2008" enabled="false" />
  </domainControllers>
</logParserSettings>

Miscellaneous

  • In the designer, all I have is what you see here, a ConfigurationSection, a ConfigurationElementCollection, and a ConfigurationElement - there are no additional configuration elements/sections/groups
  • I am not adding or removing anything at run-time
  • I have tried both generating and not generating the Singleton property, but neither the singleton instance nor an instance of the settings class seem to work
  • For the ID attribute, the Is Key property is set to true, and all other attributes are marked true for Is Required

Has anyone ever run into this? If so, what change do I have to make to get this to work as intended?

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

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

发布评论

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

评论(1

强辩 2024-07-23 07:09:31

好吧,我通过将问题指定为特定元素,使真正的问题变得过于复杂,而事实证明,即使是 ConfigurationSection 上的简单测试属性也不会返回预期值。 我还意识到我无法访问 AppSettings,这导致了一个严重的线索。

最终原因是? 该项目被编译为类库。 配置文件仅在应用程序级别可用。 由于我使用 NUnit 来运行测试,因此测试项目中的配置文件会覆盖类库的开发文件。 这对于任何没有陷入 ASP.NET 领域的开发人员来说可能是显而易见的。

通过在测试项目的app.config中添加以下configSection

<section name="customConfigSection" type="ClassLibraryNamespace.SettingsClass, ClassLibraryNamespace"/>

并填写相应的Section,就可以正常访问数据了。

希望这能节省一些时间的调试时间。

Well, I grossly overcomplicated the true issue by specifying the problem down to a specific element, when it turns out not even a simple test attribute on the ConfigurationSection would return an expected value. I also realized that I couldn't access AppSettings, which resulted in a raging clue.

The ultimate cause? The project was compiled as a class library. Config files are only available at the application level. Since I was using NUnit to run the tests, the configuration file from the test project was overriding the development one for the class library. This is probably obvious to any developers who are not stuck in ASP.NET land.

By adding the following configSection to the app.config within the test project:

<section name="customConfigSection" type="ClassLibraryNamespace.SettingsClass, ClassLibraryNamespace"/>

and filling in the corresponding section, the data could be accessed as expected.

Hopefully, this will save someone hours of debugging.

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