【问题标题】:Custom Configuration Collection - Unrecognized element 'addService'自定义配置集合 - 无法识别的元素“addService”
【发布时间】:2011-06-26 16:18:40
【问题描述】:

来自 MSDN 的有关制作应按如下方式工作的自定义配置部分的示例,

class RemoteServiceSection : ConfigurationSection
{
    [ConfigurationProperty("remoteServices", IsDefaultCollection=false)]
    [ConfigurationCollection(typeof(RemoteServiceCollection), AddItemName="addService", ClearItemsName="clearServices",
        RemoveItemName="removeService")]
    public RemoteServiceCollection Services
    {
        get
        {
            return this["remoteServices"] as RemoteServiceCollection; 
        }
    }
}

class RemoteServiceCollection : ConfigurationElementCollection, IList<RemoteServiceElement>
{
    public RemoteServiceCollection()
    {
        RemoteServiceElement element = (RemoteServiceElement)CreateNewElement();
        Add(element); 
    }

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

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((RemoteServiceElement)element).Hostname;
    }

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

    public new IEnumerator<RemoteServiceElement> GetEnumerator()
    {
        foreach (RemoteServiceElement element in this)
        {
            yield return element; 
        }
    }

    public void Add(RemoteServiceElement element)
    { 
        BaseAdd(element, true); 
    }

    public void Clear()
    {
        BaseClear(); 
    }

    public bool Contains(RemoteServiceElement element)
    {
        return !(BaseIndexOf(element) < 0); 
    }

    public void CopyTo(RemoteServiceElement[] array, int index)
    {
        base.CopyTo(array, index); 
    }

    public bool Remove(RemoteServiceElement element)
    {
        BaseRemove(GetElementKey(element));
        return true; 
    }

    bool ICollection<RemoteServiceElement>.IsReadOnly
    {
        get { return IsReadOnly(); } 
    }

    public int IndexOf(RemoteServiceElement element)
    {
        return BaseIndexOf(element); 
    }

    public void Insert(int index, RemoteServiceElement element)
    {
        BaseAdd(index, element); 
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index); 
    }

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

class RemoteServiceElement : ConfigurationElement
{
    public RemoteServiceElement() { }

    public RemoteServiceElement(string ip, string port)
    {
        this.IpAddress = ip;
        this.Port = port; 
    }

    [ConfigurationProperty("hostname", IsKey = true, IsRequired = true)]
    public string Hostname
    {
        get
        {
            return (string)this["hostname"];
        }
        set
        {
            this["hostname"] = value;
        }
    }
    [ConfigurationProperty("ipAddress", IsRequired = true)]
    public string IpAddress
    {
        get
        {
            return (string)this["ipAddress"];
        }
        set
        {
            this["ipAddress"] = value;
        }
    }
    [ConfigurationProperty("port", IsRequired = true)]
    public string Port
    {
        get
        {
            return (string)this["port"];
        }
        set
        {
            this["port"] = value;
        }
    }
}

}

我收到“无法识别的元素 'addService'”的错误消息。我想我完全遵循了 MSDN 文章。可以在这里找到 - http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx

提前感谢您的帮助。这就是我在 app.config 中写的(当然,括号不会出现在这里?):

 <remoteServices>
   <addService hostname="xxxxxxx" ipAddress="xxx.x.xxx.xx" port="xxxx" >
 </remoteServices>

这里是 app.config 所要求的,只是出于隐私目的而删除了特定名称,它们只是字符串:

<configuration>
<configSections>
  <section name="remoteServices" type="AqEntityTests.RemoteServiceSection, 
     AqEntityTests" allowLocation="true" allowDefinition="Everywhere"/>
  </configSections>
  <remoteServices>
   <addService hostname="xxxxxx.xxxxxxx.com" 
            ipAddress="xxx.x.xxx.xx" 
            port="xx" />
  </remoteServices>

【问题讨论】:

  • 需要查看app.config文件。
  • 请发布 app.config,我怀疑,您可能在 app.config 开头缺少自定义部分定义。
  • 这个问题解决了吗?我认为接受的答案只是对有关该问题的更多数据的澄清。

标签: c# configuration app-config configurationsection


【解决方案1】:

为了后代:

您的配置应如下所示:

<configuration>
  <configSections>
    <section name="remoteServices" type="AqEntityTests.RemoteServiceSection, 
        AqEntityTests" allowLocation="true" allowDefinition="Everywhere"/>
  </configSections>

  <remoteServices>
    <remoteServices>
      <addService hostname="xxxxxx.xxxxxxx.com" 
         ipAddress="xxx.x.xxx.xx" 
         port="xx" />
    </remoteServices>
  </remoteServices>
</configuration>

为什么?

你添加到节点:

<configSections> 

自定义部分命名:

name="remoteServices"

有类型

type="AqEntityTests.RemoteServiceSection

然后在代码中,将属性添加到自定义部分:

[ConfigurationProperty("remoteServices", IsDefaultCollection=false)]

意味着您在节点内部创建了具有相同名称的节点。因此,您收到错误“无法识别的元素'addService'”。只是编译器通知你这样的元素不应该在那个节点中。

自定义配置快速学习的两个链接:
Custom Configuration Sections for Lazy Coders
How to create sections with collections

【讨论】:

    【解决方案2】:

    您也可以考虑使用未命名的默认集合,正如我提到的 here

    这允许您以您建议的方式添加项目。

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 2015-04-29
      • 2021-09-18
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 2013-10-31
      相关资源
      最近更新 更多