Peter Wright
Peter Wright

Reputation: 79

Nesting custom configuration element collections

I'm trying to implement a custom configuration solution for an asp.net project I'm currently involved in.

Here is the configuration declaration

    <sectionGroup name="WebsiteConfig" type="{namespace}.{class}, {assembly}">
        <section name="Languages" type="{namespace}.{class}, {assembly}"/>
        <section name="LinkFormats" type="{namespace}.{class}, {assembly}"/>
        <section name="Countries" type="{namespace}.{class}, {assembly}"/>
    </sectionGroup>

with this being the actual configuration I'm trying to use

<WebsiteConfig>

    <Languages>
        <Language code="en" domain="...">
            <Theme .../>
            <SiteMap ..."/>
        </Language>
        <Language code="de" domain="...">
            <Theme .../>
            <SiteMap .../>
        </Language>
    </Languages>

    <Countries>
        <Country Code="UK">
            <Files>
                <File name="..." fileUrl="..." enabled="true" />
                <File ... />
            </Files>
            <Messages>
                <Message Enabled="true" Message="..." />
                <Message ... />
            </Messages>
        </Country>
        <Country Code="...">....</Country>
    </Countries>

    <LinkFormats UseRewrites="false">
        <Link name="..." format="..." formatRewrite=".../"/>
        <link .... />
    </LinkFormats>

</WebsiteConfig>

The problem I'm having is that the Files and Messages collections inside of the Country element (ConfigurationElement) throws an Unrecognised element 'File' etc.

My country element has the following properties for Files and Messages

    [ConfigurationProperty("Files")]
    public FilesSection Files
    {
        get
        {
            return (FilesSection)this["Files"];
        }
        set
        {
            this["Files"] = (object)value;
        }
    }

    [ConfigurationProperty("Messages")]
    public MessagesSection Messages
    {
        get
        {
            return (MessagesSection)this["Messages"];
        }
        set
        {
            this["Messages"] = (object)value;
        }
    }

FilesSection and MessagesSection are both derived from ConfigurationElement with a default collection of type x, which is a collection of items derived from ConfigurationElement.

Does anyone have an insight into where I've gone wrong?

Do I need to turn Countries and Country into SectionGroup's and then turn Files and Messages into Section's?

Upvotes: 3

Views: 4200

Answers (2)

cweston
cweston

Reputation: 11647

Instead of setting the internal member variable for Element Name like in the article you supplied you can instead override the ElementName property with your custom name:

public class MySettings : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MySetting();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MySetting)element).Name;
    }

    protected override string ElementName
    {
        get { return “MySetting”; }
    }

    ...

See the article here for more detail:

Upvotes: 0

Peter Wright
Peter Wright

Reputation: 79

Based on this article http://www.endswithsaurus.com/2008/11/custom-configuration-section.html I discovered that I needed to set the ElementName because a nested collection who's parent isn't a section handler will not obey the "AddItemName" for setting child elements.

Upvotes: 4

Related Questions