【问题标题】:app.config - configuration system failed to initialize with custom sectionapp.config - 配置系统无法使用自定义部分进行初始化
【发布时间】:2019-01-20 01:27:02
【问题描述】:

我有一个 web api,我已经按照以下链接在 web.config 中添加了一个自定义部分:https://msdn.microsoft.com/en-us/library/2tw134k3.aspx。我的 web.config 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please 
  visit https://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
  <configSections>
    <sectionGroup name="productGroup">
      <section
       name="product"
       type="myProject.Stuff.Api.ProductSection"
       allowLocation="true"
       allowDefinition="Everywhere"
      />
    </sectionGroup>
  </configSections>
  <appSettings>
    <!-- various app settings -->
  </appSettings>
  <productGroup>
    <product name="my name" id="1" />
    <product name="my name 2" id="2" />
  </productGroup>

<!-- other standard elements -->

</configuration>

我在 configSections 和 productGroup 中添加了。然后我将一个 c# 类定义为 ProductSection,如下所示:

使用 System.Configuration;

namespace myProject.Stuff.Api
{
    public class ProductSection : ConfigurationSection
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("id", IsRequired = true)]
        public int Id
        {
            get
            {
                return (int)this["id"];
            }
            set
            {
                this["id"] = value;
            }
        }
    }
}

我已将 2 个部分复制到我的测试项目中的 app.config 中,但是当我现在尝试调试测试时,我得到了初始化失败的错误。当我注释掉 productGroup 部分时,它就会停止抱怨。我做错了什么?

更新

作为旁注,我最终将它的结构更简单一点。由于我的“产品”部分只需要一个名称和 ID,因此它们根据 appSettings 借给键值对。所以我创建了一个这样的部分:

<configSections>
  <section name="productGroup"
           type="System..Configuration.NameValueSectionHandler"
           allowLocation="true"
           allowDefinition="Everywhere"/>
</configSections>

请注意,我更改了类型。然后我的 productGroup 看起来像这样:

<productGroup>
  <add key="product1" value="1" />
  <add key="product2" value="2" />
</productGroup>

然后我可以完全删除 ProductSection 类,并像这样引用我的产品组:

var products = ConfigurationManager.GetSection("productGroup") as NameValueCollection;

if (products.AllKeys.Contains(myProduct))
{
    var myValue = products[myProduct];
}    

【问题讨论】:

    标签: c# unit-testing asp.net-web-api web-config app-config


    【解决方案1】:

    我注意到两件事

    一个配置部分在一个配置文件中只能出现 1 次。
    完整的初始化异常显示:

    System.Configuration.ConfigurationErrorsException:配置系统初始化失败---> System.Configuration.ConfigurationErrorsException: 每个配置文件中的部分只能出现一次

    这意味着你只能有 1 个product 部分:

    <productGroup>
        <product name="my name" id="1" />       
    </productGroup>
    

    如果需要多个,则需要用唯一的部分名称声明多个部分:

    <configuration>
        <sectionGroup name="productGroup">
            <section name="product" type="myProject.Stuff.Api.ProductSection" />
            <section name="product2" type="myProject.Stuff.Api.ProductSection" />
        </sectionGroup>
    
        <!-- Other configuration elements -->
    
        <productGroup>
            <product name="my name" id="1" />
            <product2 name="my name 2" id="2" />
        </productGroup>
    </configuration>
    

    最好在app/web.config 中使用其标准程序集名称声明该部分,
    其中包括程序集的名称(不带文件扩展名)。
    如果类/部分是在外部程序集中定义的,这是必须的;这里ProductionSection 是在除主单元测试之外的程序集中定义的。

    在您的单元测试项目的App.config 文件中声明如下部分。
    (将 NameOfTheAssemblyContainingTheProductSectionClass 替换为您的程序集的名称。

    <section
        name="product"
        type="myProject.Stuff.Api.ProductSection, NameOfTheAssemblyContainingTheProductSectionClass"             
        />
    

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 2017-04-29
      • 1970-01-01
      • 2018-03-23
      • 2014-03-08
      • 2011-10-02
      • 2013-11-02
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多