.net自定义配置节未加载

时间:2018-11-23 06:56:44

标签: c# asp.net configsection

我正在尝试创建一个自定义配置部分,用于按区域存储api设置。这是用于asp.net Web API。

但是,我无法使我的自定义配置部分起作用。当我运行单元测试时,它说:

  

消息:System.Configuration.ConfigurationErrorsException:无法识别的属性'id'“。

public class MyApiConfigSection : ConfigurationSection
{
    public static Lazy<MyApiConfigSection> Configuration = new Lazy<MyApiConfigSection>(() =>
        ConfigurationManager.GetSection("myApi/myApi.regions") as MyApiConfigSection);

    [ConfigurationProperty("myApiSettings")]
    public MyApiElement MyApiSettings
    {
        get
        {
            return (MyApiElement)this["myApiSettings"];
        }
        set
        {
            this["myApiSettings"] = value;
        }
    }
}

public class MyApiElement : ConfigurationElement
{
    [ConfigurationProperty("apiVersions", IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(ApiVersionsCollection))]
    public ApiVersionsCollection ApiVersions
    {
        get
        {
            ApiVersionsCollection gtVersions =
                (ApiVersionsCollection)base["apiVersions"];

            return gtVersions;
        }
        set
        {
            ApiVersionsCollection apiVersions = value;
        }
    }
}

public class ApiVersionsCollection : ConfigurationElementCollection
{
    public ApiVersionsCollection() { }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ApiVersionElement();
    }

    protected override Object GetElementKey(ConfigurationElement element)
    {
        return ((ApiVersionElement)element).Id;
    }

    public ApiVersionElement this[int index]
    {
        get
        {
            return (ApiVersionElement)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    new public ApiVersionElement this[string name]
    {
        get
        {
            return (ApiVersionElement)BaseGet(name);
        }
    }
}

public class ApiVersionElement : ConfigurationElement
{
    public ApiVersionElement() { }

    public ApiVersionElement(string id, string apiKey, RegionUrlsCollection regionUrls)
    {
        this.Id = id;
        this.ApiKey = apiKey;
        this.RegionUrls = regionUrls;
    }

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

    [ConfigurationProperty("apiKey", IsRequired = true)]
    public string ApiKey
    {
        get { return (string)this["apiKey"]; }
        set { this["apiKey"] = value; }
    }

    [ConfigurationProperty("regionUrls", IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(RegionUrlsCollection))]
    public RegionUrlsCollection RegionUrls
    {
        get
        {
            RegionUrlsCollection regionUrls =
                (RegionUrlsCollection)base["regionUrls"];

            return regionUrls;
        }
        set
        {
            RegionUrlsCollection regionUrls = value;
        }
    }
}

public class RegionUrlsCollection : ConfigurationElementCollection
{
    public RegionUrlsCollection() { }

    protected override ConfigurationElement CreateNewElement()
    {
        return new RegionElement();
    }

    protected override Object GetElementKey(ConfigurationElement element)
    {
        return ((RegionElement)element).Name;
    }

    public RegionElement this[int index]
    {
        get
        {
            return (RegionElement)BaseGet(index);
        }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    new public RegionElement this[string name]
    {
        get
        {
            return (RegionElement)BaseGet(name);
        }
    }
}

public class RegionElement : ConfigurationElement
{
    public RegionElement() { }

    public RegionElement(string name, string url)
    {
        this.Name = name;
        this.Url = url;
    }

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("url", IsRequired = true)]
    public string Url
    {
        get { return (string)this["url"]; }
        set { this["url"] = value; }
    }
}

App.config中的config部分如下:

其他类似部分正在正确加载-唯一的区别是这里有2个嵌套集合。

      <myApi>
        <myApi.regions>
          <myApiSettings>
            <apiVersions id="1" apiKey="sample_api_key_1">
              <regionUrls>
                <region name="amer" url="" />
                <region name="apac" url="" />
                <region name="emea" url="" />
              </regionUrls>
            </apiVersions>
            <apiVersions id="2" apiKey="sample_api_key_2">
              <regionUrls>
                <region name="amer" url="" />
                <region name="apac" url="" />
                <region name="emea" url="" />
              </regionUrls>
            </apiVersions>
          </myApiSettings>
        </myApi.regions>
      </myApi>

0 个答案:

没有答案