Failed to read in my .config section.

NOTE: this is very similar to this SO question , but I need more help.

I am trying to make the next section in my .config file, but I get an exception when trying to access this section.

.config file

<configSections>
    <section name="foos" type="Ackbar.Mvc.Models.Foo.FooCollection, Ackbar.Mvc" requirePermission="false"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" requirePermission="false" />
</configSections>

<foos>
    <add name="aaa" something="zzz"/>
    <add name="bbb" something="yyy"/>
    <add name="ccc" something="xxx"/>
</foos>

So that means I need to do two classes

Classes

public class FooCollection : ConfigurationElementCollection
{
    ... with my custom overrides, etc. ...
}

and

public class FooElement : ConfigurationElement
{
    [ConfigurationProperty("Name", IsRequired = true)]
    public string Name { .. }

    [ConfigurationProperty("Something ", IsRequired = true)]
    public string Something { .. }

    [ConfigurationProperty("IsDefault ", IsRequired = false, DefaultValue = false)]
    public bool IsDefault { .. }
}

Kewl. Now when I do the following ....

var whatever = ConfigurationManager.GetSection("foos") throws the following exception: -

An error occurred while creating the configuration section handler for foos: Type 'Ackbar.Mvc.Models.Foos.FooCollection' is not inherited from 'System.Configuration.IConfigurationSectionHandler'.

Can someone help me? I do not want to wrap the INSIDE collection of the parent section.

Greetings :)

+3
2

IConfigurationSectionHandler. .

FooCollection .

IsDefaultCollection.

+2

FooCollection , ConfigurationSection.

ConfigurationElementCollection , -. FooSection .

<configSections>
    <section name="foos" type="Ackbar.Mvc.Models.Foo.FooSection, Ackbar.Mvc" requirePermission="false"/>
</configSections>

<foos>
    <add name="aaa" something="zzz"/>
    <add name="bbb" something="yyy"/>
    <add name="ccc" something="xxx"/>
</foos>

:

public class FooSection : ConfigurationSection
{
    [ConfigurationProperty("", IsDefaultCollection=true)]
    public FooCollection Foos => (FooCollection)this[""];

    // optionally add convenience accessors to the 'Foos' collection
}
0

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


All Articles