自定义配置集合 - 无法识别的元素'addService'

时间:2011-02-14 02:31:42

标签: c# configuration app-config configurationsection

关于制作应如下工作的自定义配置部分的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,为了隐私目的x出特定的名称,它们只是字符串:

<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>

2 个答案:

答案 0 :(得分: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)]

这意味着您在节点内创建了具有相同名称的节点。因此,您收到了错误&#34;无法识别的元素&#39; addService&#39;&#34;。只是编译通知您这样的元素不应该在该节点中。

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

答案 1 :(得分:0)

在提及here

时,您可能还会考虑使用未命名的默认集合

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