2

I am trying to make a fairly simple custom configuration section. My class is:

namespace NetCenterUserImport
{
    public class ExcludedUserList : ConfigurationSection
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get { return (string)base["name"]; }
        }

        [ConfigurationProperty("excludedUser")]
        public ExcludedUser ExcludedUser
        {
            get { return (ExcludedUser)base["excludedUser"]; }
        }

        [ConfigurationProperty("excludedUsers")]
        public ExcludedUserCollection ExcludedUsers
        {
            get { return (ExcludedUserCollection)base["excludedUsers"]; }
        }
   }

   [ConfigurationCollection(typeof(ExcludedUser), AddItemName = "add")]
   public class ExcludedUserCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ExcludedUserCollection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ExcludedUser)element).UserName;
        }
    }

    public class ExcludedUser : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string UserName
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
    }
}

My app.config is:

  <excludedUserList name="test">
<excludedUser name="Hello" />
<excludedUsers>
  <add name="myUser" />
</excludedUsers>

When I attempt to get the custom config section using:

var excludedUsers = ConfigurationManager.GetSection("excludedUserList");

I get an exception saying

"Unrecognized attribute 'name'."

I'm sure I'm missing something simple, but I've looked at a dozen examples and answers on here and can't seem to find where I'm going wrong.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
DrewB
  • 1,503
  • 1
  • 14
  • 28
  • 1
    Is `excludedUserList` the start of the section? If so, i dont think you can have an attribute attached to it. – rbm Sep 11 '15 at 14:16
  • Well not really - check here: http://stackoverflow.com/questions/9187523/adding-custom-attributes-to-custom-provider-configuration-section-in-app-config – rbm Sep 11 '15 at 14:20
  • Even without the attribute in excludedUserList I get the same error. I only added to determine exactly when the error appeared. I added a custom attribute to the section, then added the custom property to the sction and when those both worked, added the custom collection back in and everything broke again. – DrewB Sep 11 '15 at 14:26

1 Answers1

2

In ExcludedUserCollection.CreateNewElement method you are creating a ExcludedUserCollection instance, it should be a single element such as:

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

Changing the method as above worked for me.

Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40
  • 1
    Perfect! Thank you. I knew it was something simple I was overlooking, but the only other developer in my office had a stroke, so I have no one to peer review things like this ATM. – DrewB Sep 11 '15 at 14:41