Pergunta

I'm creating a custom configuration section but I keep getting a Attribute Not Recognized error when trying to get the section.

I'm pretty sure it's some dumb typo - hopefully someone here can spot it.

Code:

<configSections>
  <section name="ClientFilterSettings" type="ESPDB.Config.ClientFilterSettings, ESPDB"/>
</configSections>
<ClientFilterSettings>
  <AllowedIPs>
    <IPAddress IP="255.255.255.255"/>
  </AllowedIPs>
</ClientFilterSettings>

Section:

namespace ESPDB.Config
{
    public class ClientFilterSettings : ConfigurationSection
    {
        private static ClientFilterSettings _settings = 
            ConfigurationManager.GetSection(typeof(ClientFilterSettings).Name) as ClientFilterSettings
            /*?? new ClientFilterSettings()*/;

        private const string _allowedIPs = "AllowedIPs";

        public static ClientFilterSettings Settings
        {
            get { return _settings; }
        }

        [ConfigurationProperty(_allowedIPs, IsRequired = true)]
        [ConfigurationCollection(typeof(IPAddressCollection))]
        public IPAddressCollection AllowedIPs
        {
            get { return (IPAddressCollection)this[_allowedIPs]; }
            set { this[_allowedIPs] = value; }
        }
    }
}

Collection:

namespace ESPDB.Config
{
    public class IPAddressCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new IPAddressCollection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as IPAddressElement).IP;
        }

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

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

        public new IPAddressElement this[string responseString]
        {
            get { return (IPAddressElement)BaseGet(responseString); }
            set
            {
                if (BaseGet(responseString) != null)
                {
                    BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
                }
                BaseAdd(value);
            }
        }
    }
}

Element

namespace ESPDB.Config
{
    public class IPAddressElement : ConfigurationElement
    {
        private const string _ip = "IP";

        [ConfigurationProperty(_ip, IsKey = true, IsRequired = true)]
        public string IP
        {
            get { return this[_ip] as string; }
            set { this[_ip] = value; }
        }
    }
}
Foi útil?

Solução

There are multiple problems in your code.

1). You are recursively creating objects of ClientFilterSettings. Remove the following code, its not required.

private static ClientFilterSettings _settings =
                 ConfigurationManager.GetSection(typeof(ClientFilterSettings).Name) as ClientFilterSettings         /*?? new ClientFilterSettings()*/; 
public static ClientFilterSettings Settings     
{       
      get { return _settings; }     
}

2). Change the attribute from

[ConfigurationCollection(typeof(IPAddressCollection))]

TO

[ConfigurationCollection(typeof(IPAddressElement), AddItemName = "IPAddress", CollectionType = ConfigurationElementCollectionType.BasicMap)]

3). You are creating collection object within a collection. Modify the code below

return new IPAddressCollection();

WITH

return new IPAddressElement();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top