views:

34

answers:

1

In ASP.NET, when creating a custom config section, how do I limit the number of a particular section that can be declared?

For example, if in web.config, I repeat the appSettings section...

<appSettings /> 
<appSettings />

...I get an exception when the configuration loads.

If I do the same with my section...

<employer /> 
<employer />

...it allows it, but I don't want it to.

My section code (C#) looks like this:

    [ConfigurationProperty("employer", IsRequired = true)]
    public EmployerElement Employer
    {
        get
        {
            return (EmployerElement)this["employer"];
        }
        set
        { this["employer"] = value; }
    }

Any help appreciated, thanks.

A: 

I think each configuration property can only appear once in a section. I tested it in my code and I got an exception. The above code is only a property. Can you post the completed code of config section and EmployerElement configuration element?

Mehdi Golchin
Sorry, my fault. I had the configuration in an external file, so when it was updated it wasn't causing an automatic application re-start. Obviously, duplicating <appSettings> in web.config was causing a restart. I now get the error in both scenarios, so all is good. Thanks.
Moose Factory