【问题标题】:Custom config section is causing Configuration to fail to load自定义配置部分导致配置无法加载
【发布时间】:2016-07-19 20:15:42
【问题描述】:

我有added a link to my app.config file to my unit test project,它可以工作,我可以看到列出的配置值。但是,当我将此处讨论的自定义部分 (How to get the values of a ConfigurationSection of type NameValueSectionHandler) 添加到我的 XML 时,ConfigurationManager 在调试器中显示为处于无效状态。

我的 app.config 结束如下:

MSDN 说我们不应该使用旧的 pre .NET 2 配置类,因为它们已被弃用。所以我不确定这里有什么问题?

【问题讨论】:

  • 在该部分,尝试使用<section name="MyTest" type="System.Configuration.AppSettingsSection, System.Configuration" />。也不要使用图片。
  • 第二个元素控制什么 - 你有链接吗?
  • 这是对其程序集的引用。我已经在我的项目中找到了它。
  • 似乎没有任何区别
  • 那么请告诉我们,您如何称呼ConfigurationManager。以及您如何阅读自定义部分的值。

标签: c# .net app-config


【解决方案1】:

啊哈...我原以为没有记录任何错误,但发现报告了这一点,这将其清除:

System.Configuration.ConfigurationErrorsException:配置 系统初始化失败---> System.Configuration.ConfigurationErrorsException:只有一个 每个配置文件允许的元素,如果存在必须 成为根元素的第一个子元素。

【讨论】:

  • 我想你可能想要type="System.Configuration.NameValueSectionHandler" 而不是AppSettingsSection
  • 根据 MSDN,这已被弃用,尽管它令人困惑,因为很多示例都使用它。
猜你喜欢
  • 2013-09-07
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
  • 1970-01-01
  • 2012-10-20
相关资源
最近更新 更多