【问题标题】:C# custom configuration: "The entry '' has already been added."C# 自定义配置:“已添加条目 ''。”
【发布时间】:2017-10-10 10:54:21
【问题描述】:

我做了一个小的自定义配置设置,但我不断收到错误消息“已添加条目 ''。”当我尝试使用我的自定义集合时。我的代码如下所示。

问题来自我的标签。

我没有看到我缺少什么,因为我已经实现了相同的东西,而且这个效果很好。

如果有帮助的话,我的 .NET 版本是 4.0。

有问题的应用配置部分:

<WorkersCollectionSection>
<WorkersList>
  <Worker name="Category" isEnabled="false"
        assemblyNamespace="xxxxxx.xxxxxxxxxxxxxxx.Models.Category"
        queueName="CategorQueue"
        saveToFolder="false">
    <HandlesList>
      <Handle name="xxxxxxx" isEnabled="true"/>
      <Handle name="yyyyyyy" isEnabled="true"/>
      <Handle name="zzzzzzzzzz" isEnabled="true"/>
    </HandlesList>
  </Worker>
 <WorkersList>
<WorkersCollectionSection>

属性的定义:

    [ConfigurationProperty("HandlesList")]
    [ConfigurationCollection(typeof(WorkerCollection), AddItemName = "Handle")]
    public HandleCollection HandleCollection
    {
        get { return (HandleCollection) base["HandlesList"]; }
    }

标签代码:` public class HandleCollection : ConfigurationElementCollection, IEnumerable {

    public HandleCollection()
    {
        HandleElement handle = (HandleElement)CreateNewElement();
        BaseAdd(handle);
    }

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

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

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

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

    public new HandleElement this[string Name]
    {
        get { return (HandleElement)BaseGet(Name); }
    }

    public int IndexOf(HandleElement handle)
    {
        return BaseIndexOf(handle);
    }

    public void Add(HandleElement url)
    {
        BaseAdd(url);
    }

    protected override void BaseAdd(ConfigurationElement element)
    {
        BaseAdd(element, false);
    }

    public void Remove(HandleElement handle)
    {
        if (BaseIndexOf(handle) >= 0)
            BaseRemove(handle);
    }

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

    public void Remove(string name)
    {
        BaseRemove(name);
    }

    public void Clear()
    {
        BaseClear();
    }

    IEnumerator<ConfigurationElement> IEnumerable<ConfigurationElement>.GetEnumerator()
    {
        return (from i in Enumerable.Range(0, this.Count)
                select this[i])
            .GetEnumerator();
    }

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

    public static explicit operator HandleCollection(Dictionary<string, string> v)
    {
        throw new NotImplementedException();
    }

    public static explicit operator HandleCollection(ConfigurationSection v)
    {
        throw new NotImplementedException();
    }
}`

列表内句柄元素的代码:

 public class HandleElement: ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 0, MaxLength = 60)]
    public string Name
    {
        get { return base["name"] as string; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("isEnabled", IsRequired = true)]
    public bool IsEnabled
    {
        get { return (bool)base["isEnabled"]; }
        set { base["isEnabled"] = value; }
    }

}

【问题讨论】:

  • 在配置的&lt;HandlesList&gt; 部分,第一个元素有两个左尖括号:`'
  • 这是一个小贴士。我的错。
  • 在添加之前,您是否在 app.config 文件中注册了自定义配置部分

标签: c# app-config configurationmanager


【解决方案1】:

似乎问题在于我没有在集合添加方法中检查以检查我正在添加的元素是否具有与空元素不同的键。我将我的集合构造函数更改为:

 public HandleCollection()
    {
        HandleElement handle = (HandleElement)CreateNewElement();
        if (handle.Name != "")
            BaseAdd(handle);
    }

现在它似乎工作正常。

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 2014-01-10
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2015-07-18
    相关资源
    最近更新 更多