0

我有一个 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 中添加了。然后我将 ac# 类定义为 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;
            }
        }
    }
}

我已经将这两个部分复制到我的测试项目中的 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];
}    
4

1 回答 1

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使用完全限定的 assemblyname来声明节,
其中包括程序集的名称(不带文件扩展名)。
如果类/部分是在外部程序集中定义的,这是必须的;这里ProductionSection是在除主要单元测试之一之外的程序集中定义的。

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

<section
    name="product"
    type="myProject.Stuff.Api.ProductSection, NameOfTheAssemblyContainingTheProductSectionClass"             
    />
于 2018-08-13T13:34:17.503 回答