c# - 在同一 App.Config 中使用 ConnectionStrings 和自定义 ConfigSections

标签 c# .net app-config

我有一个按预期工作的自定义 configSection。但是,当我添加“connectionStrings”部分时,我收到错误:

Configuration system failed to initialize

上线:

StencilObjects so = ConfigurationManager.GetSection( "stencilObjects" ) as StencilObjects;

这是配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="connection" connectionString="foo"/>
  </connectionStrings>
  <configSections>
    <section name="stencilObjects" type="Stencil.Configuration.StencilObjects, Stencil.Configuration"/>
  </configSections>
  <stencilObjects>
    <tableData>
      <table schema="Auth" name="SecurityQuestion" />
    </tableData>
  </stencilObjects>
</configuration>

使用自定义配置部分有什么限制吗?这是否不允许使用连接字符串?

再次,当我删除连接字符串时,应用程序按预期运行。

知道发生了什么吗?

最佳答案

我还没有找到一个链接来通过明确的声明来支持这一点,但我总是在文件顶部使用 configSections ,没有任何问题。尝试这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="stencilObjects" type="Stencil.Configuration.StencilObjects, Stencil.Configuration"/>
  </configSections>
  <connectionStrings>
    <add name="connection" connectionString="foo"/>
  </connectionStrings>
  <stencilObjects>
    <tableData>
      <table schema="Auth" name="SecurityQuestion" />
    </tableData>
  </stencilObjects>
</configuration>

configSections 绝对不需要位于它所描述的部分之前。 connectionStrings 可以介于两者之间。

关于c# - 在同一 App.Config 中使用 ConnectionStrings 和自定义 ConfigSections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7151082/

相关文章:

.net - 加密 connectionStrings 部分 - app.config 的实用程序

c# - 多个事件的一个处理程序

c# - 检查对象列表中是否存在对象

c# - 奇怪,regex.split方法匹配一个null元素

c# - 需要避免数字签名公共(public)证书 BinarySecurityElement 被 mtom 编码

c# - App.config 中的配置未正确提取

C# 类型需要特定类的子类以及接口(interface)

c# - MVC 单选按钮选择一个

c# - 如何在插件中使用 NLog

visual-studio - 如何将app.config放到app.config下面?