【问题标题】:Custom configuration section in app.config -> Unrecognized element 'add'app.config 中的自定义配置部分 -> 无法识别的元素“添加”
【发布时间】:2021-09-18 09:40:14
【问题描述】:

所以,我正在尝试为我的 app.config 创建一个自定义配置部分,它将处理以下模式:

  <domainSolutionSection>
    <domainSolutionGroups>
      <Group groupname="Legacy">
        <add name="Old App #1"/>
        <add name="Old App #2" />
      </Group>
      <Group groupname="Modern">
        <add name="New App #1" />
        <add name="New App #2" />
        <add name="New App #3" />
      </Group>
    </domainSolutionGroups>
  </domainSolutionSection>

我花了一整天的时间试图让它发挥作用。
现在,我收到一个错误:

System.Configuration.ConfigurationErrorsException:无法识别元素“添加”。

这是我的 DomainSolutionSection 库中的类:

public class DomainSolutionSection : ConfigurationSection
{
    [ConfigurationProperty("domainSolutionGroups")]
    public DomainSolutionGroupCollection DomainSolutionGroups
    {
        get
        {
            return (DomainSolutionGroupCollection) this["domainSolutionGroups"];
        }
        set
        {
            this["domainSolutionGroups"] = value;
        }
    }
}

[ConfigurationCollection(typeof(GroupConfigElement))]
public class DomainSolutionGroupCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new GroupConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((GroupConfigElement)element).GroupName;
    }

    protected override string ElementName
    {
        get { return "Group"; }
    }

    protected override bool IsElementName(string elementName)
    {
        return !String.IsNullOrEmpty(elementName) && elementName == "Group";
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    public GroupConfigElement this[int index]
    {
        get
        {
            return base.BaseGet(index) as GroupConfigElement;
        }
    }

    public new GroupConfigElement this[string key]
    {
        get
        {
            return base.BaseGet(key) as GroupConfigElement;
        }
    }
}
   
public class GroupConfigElement : ConfigurationElement
{
    [ConfigurationProperty("groupname", IsRequired = true, IsKey = true)]
    public string GroupName
    {
        get { return (string)this["groupname"]; }
        set { this["groupname"] = value; }
    }

    public PhraseCollection Phrases
    {
        get { return (PhraseCollection) base["Groups"]; } 
    }
}

[ConfigurationCollection(typeof(PhraseConfigElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class PhraseCollection: ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new PhraseConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((PhraseConfigElement)element).Name;
    }
}

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

没有找到在这个嵌套级别使用“添加”属性的任何示例,所以很明显,我没有正确“连接”“组”。任何人都可以提供任何指导吗?

谢谢!

【问题讨论】:

    标签: c# .net app-config


    【解决方案1】:

    实际上,我大约 10 分钟前才想出答案。基本上,我并没有真正将 PhraseCollection 正确“连接”到 GroupConfigElement ......我也没有完全充实 PhraseCollection。:

        public class GroupConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("groupname", IsRequired = true, IsKey = true)]
        public string GroupName
        {
            get { return (string)this["groupname"]; }
            set { this["groupname"] = value; }
        }
    
        [ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
        public PhraseCollection Phrases
        {
            get { return (PhraseCollection) base[""]; }
        }
    }
    
    [ConfigurationCollection(typeof(PhraseConfigElement))]
    public class PhraseCollection: ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new PhraseConfigElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((PhraseConfigElement)element).Name;
        }
    
        public PhraseConfigElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as PhraseConfigElement;
            }
        }
    
        public new PhraseConfigElement this[string key]
        {
            get
            {
                return base.BaseGet(key) as PhraseConfigElement;
            }
        }
    }
    
    public class PhraseConfigElement: ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
    }
    

    }

    关键修复是“短语”定义......然后我必须将这两个公共方法添加到“短语集合”定义中。

    谢谢!

    【讨论】:

      【解决方案2】:

      很抱歉,这不是您问题的直接答案。我没有时间分解你所做的事情,但这里有一个类似的例子,说明我过去做过的事情,并且有效。也许这里有什么可以帮忙的。

      myconfig.config:

      <?xml version="1.0"?>
      <myconfig>
        <moduleIPAccessRules>
          <allowRules>
            <add module="ModuleA"   subnetIP="172.16.0.0"     subnetMask="255.255.0.0"      name="ModuleA intranet users" />
            <add module="ModuleA"   subnetIP="127.0.0.0"      subnetMask="255.255.0.0"      name="ModuleA local box" />
      
            <add module="ModuleB"     subnetIP="172.16.0.0"     subnetMask="255.255.0.0"      name="ModuleB intranet users" />
            <add module="ModuleB"     subnetIP="127.0.0.0"      subnetMask="255.255.0.0"      name="ModuleB local box" />
            <add module="ModuleB"     subnetIP="123.45.67.89"   subnetMask="255.255.255.255"  name="some remote machine" />
          </allowRules>
        </moduleIPAccessRules>
      </myconfig>
      

      支持类:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Configuration;
      
      namespace Myproject.Configuration
      {
          public class IPAccessSection : ConfigurationSection
          {
              public static IPAccessSection GetConfig()
              {
                  return ConfigurationManager.GetSection("myconfig/moduleIPAccessRules") as IPAccessSection;
              }
      
              [ConfigurationProperty("allowRules")]
              public AllowRuleCollection AllowRules
              {
                  get { return this["allowRules"] as AllowRuleCollection; }
              }
          }
      
          public class AllowRuleCollection : ConfigurationElementCollection
          {
              public AllowRule this[int index]
              {
                  get
                  {
                      return base.BaseGet(index) as AllowRule;
                  }
                  set
                  {
                      if (base.BaseGet(index) != null)
                          base.BaseRemoveAt(index);
      
                      this.BaseAdd(index, value);
                  }
              }
      
              protected override ConfigurationElement CreateNewElement()
              {
                  return new AllowRule();
              }
      
              protected override object GetElementKey(ConfigurationElement element)
              {
                  return ((AllowRule)element).Name;
              }
          }
      
          public class AllowRule : ConfigurationElement
          {
              [ConfigurationProperty("name", IsRequired = true)]
              public string Name
              {
                  get { return this["name"] as string; }
              }
      
              [ConfigurationProperty("module", IsRequired = true)]
              public string Module
              {
                  get { return this["module"] as string; }
              }
      
              [ConfigurationProperty("subnetIP", IsRequired = true)]
              public string SubnetIP
              {
                  get { return this["subnetIP"] as string; }
              }
      
              [ConfigurationProperty("subnetMask", IsRequired = true)]
              public string SubnetMask
              {
                  get { return this["subnetMask"] as string; }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-03-17
        • 2016-01-17
        • 2015-04-29
        • 1970-01-01
        • 2020-08-30
        • 2011-06-26
        • 2011-11-14
        • 2022-01-18
        • 2012-10-04
        相关资源
        最近更新 更多