Wednesday, 25 April 2012

Custom Configuration Files aaaaaaahh

If anyone has done this, you might have come across all sorts of pain. I was trying to do a basic custom configuration like this:
<sessioncomparersection>
   <environments>
        <add connstring="blah" displayname="blah"></add>
   </environments>
</sessioncomparersection>
I started with a simple configuration section with a single property like this and it all worked fine:
<sessioncomparersection environments="test">
</sessioncomparersection>
I then added the ConfigurationElementCollection derived class for the list of environments and created a specialised ConfigurationElement class for the 'add' data in the collection exactly as per the examples I found on the web. Sadly, I then started getting ConfigurationErrorsException "unrecognized element 'environments'" when it tried to load the configuration. Sadly this is caused by many things, none of which helped me.
On a whim, I removed the ConfigurationPropertyCollection from the top level section (which was now empty but which was returned by an override method in my ConfigurationSection top level class) and this made it all work again!
I guess if you need the collection at the top level, you need to add a reference to the collection property (Environments in my case) into the property collection so it knows that it is present. This is despite the fact I had marked it up with ConfigurationProperty.

Post a Comment