Nesting collections of custom configuration items

I am trying to implement a custom solution for configuring an asp.net project that I am currently participating 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>

as this is the actual configuration i am 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 am facing is that the collections of files and messages inside the Country (ConfigurationElement) element throws an Unrecognized element 'File', etc.

My country item 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 produced from a ConfigurationElement with a default set of type x, which is a collection of items derived from a ConfigurationElement.

Does anyone have any idea where I got it wrong?

, ?

+3
2

- Element Name, , ElementName :

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"; }
    }

    ...

. :

0

Source: https://habr.com/ru/post/1777031/


All Articles