app.config-配置系统无法使用自定义部分初始化

时间:2018-08-13 10:29:19

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

我有一个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部分注释掉时,它将停止抱怨。我做错了什么?

UPDATE

作为旁注,我最终对其结构进行了更简单的配置。由于我的“产品”部分只需要一个名称和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];
}    

1 个答案:

答案 0 :(得分:0)

我注意到两件事

配置部分只能在配置文件中出现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"             
    />
相关问题