.NET配置节设计器 - 我的收藏在哪里? [英] .NET Configuration Section Designer - Where is my collection?

查看:162
本文介绍了.NET配置节设计器 - 我的收藏在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET的配置节设计器来构建一个简单的 ConfigurationElementCollection

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.

不幸的是,即使我在第一次使用Intellisense时,系统会自动生成代码。在配置部分向集合添加了元素,它们在运行时不存在;它显示集合的 Length 属性为0 - 即集合是空的,即使,如你可以看到在我的示例XML,我已经清楚地把它们:

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:

<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 code> ConfigurationElement - 没有其他配置元素/部分/组

  • 我没有在运行时添加或删除任何内容

  • 我尝试了生成和不生成 Singleton 属性,但是单例实例和设置类的实例似乎都不工作

  • 对于ID属性, Is Key 属性设置为true,所有其他属性标记为true Is必需

  • 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
    • 推荐答案

      那么,我真的很复杂的真实的通过将问题指定到特定的元素,当事实证明甚至在 ConfigurationSection 上的一个简单的测试属性将返回一个期望的值。我也意识到我不能访问 AppSettings ,这导致了一个肆虐的线索。

      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.

      最终原因?该项目被编译为类库。配置文件仅在应用层可用。因为我使用NUnit来运行测试,测试项目的配置文件覆盖了类库的开发。这对于没有停留在ASP.NET环境中的开发人员来说是显而易见的。

      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.

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

      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.

      这篇关于.NET配置节设计器 - 我的收藏在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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