自定义配置集合中无法识别的元素异常

时间:2019-12-02 17:33:57

标签: c# configuration

我遇到了一个自定义配置集合的问题,我查看了许多其他人的尝试,甚至查看了我的旧代码(有效!),所以我在这里很茫然,我希望另外一对为何对这种方法不起作用可能有一些新的见解。

我不断收到System.Configuration.ConfigurationErrorsException: 'Unrecognized element 'address'.

这是相关的web.config。

<configSections>
    <section name="tablesRequests" type="TableRequests.Web.TablesRequestsConfigurationSection, TableRequests.Web" allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<tablesRequests>
    <addresses>
        <address email="someone@somewhere.com" type="to" />
        <address email="someonelse@somewhere.com" type="cc" />
        <address email="nooneimportant@somewhere.com" type="bcc" />
    </addresses>
</tablesRequests>

这是配置部分

public class Configuration
{
    private static TablesRequestsConfigurationSection _config;

    private static TablesRequestsConfigurationSection Config
    {
        get
        {
            if (_config == null)
            {
                _config = (TablesRequestsConfigurationSection)ConfigurationManager.GetSection("tablesRequests");

                if (_config == null)
                {
                    throw new ConfigurationErrorsException("Could not load the configuration section tablesRequests from web.config");
                }
            }

            return _config;
        }
    }

    public static RecipientsConfiguration Recipients { get { return Config.Recipients; } }
}

这是MailRecipientsConfiguration类

public class RecipientsConfiguration : ConfigurationElement
{
    [ConfigurationProperty("addresses", IsDefaultCollection = false, IsRequired = true)]
    [ConfigurationCollection(typeof(MailRecipientConfigurationCollection), 
        AddItemName = "address", 
        ClearItemsName = "clear",
        RemoveItemName = "remove",
        CollectionType =ConfigurationElementCollectionType.BasicMap)]
    public MailRecipientConfigurationCollection Addresses
    {
        get { return (MailRecipientConfigurationCollection)this["addresses"]; }
        set { this["addresses"] = value; }
    }
}

MailRecipientConfigurationCollection类

[ConfigurationCollection(typeof(MailRecipientConfiguration))]
public class MailRecipientConfigurationCollection : ConfigurationElementCollection
{
    public string Summary
    {
        get
        {
            var output = new StringBuilder();
            output.Append("recipients = [");

            for (var xx = 0; xx < this.Count; xx++)
            {
                if (xx > 0)
                {
                    output.Append("; ");
                }
                output.Append(this[xx].Summary);
            }

            output.Append("]");

            return output.ToString();
        }
    }

    internal const string PropertyName = "address";

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMapAlternate;
        }
    }
    protected override string ElementName
    {
        get
        {
            return PropertyName;
        }
    }

    protected override bool IsElementName(string elementName)
    {
        return elementName.Equals(PropertyName,
          StringComparison.InvariantCultureIgnoreCase);
    }

    public override bool IsReadOnly()
    {
        return false;
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        var recipient = (MailRecipientConfiguration)element;
        return recipient.Email;
    }

    public MailRecipientConfiguration this[int idx]
    {
        get { return (MailRecipientConfiguration)BaseGet(idx); }
    }
}

谢谢!

0 个答案:

没有答案
相关问题