Accessing an attribute on a configSection in a web.config?

Questions


Solution: Just to add the solution: sectionGroups do not seem to have attributes. The proper way seems to have a ConfigurationSection as the parent and ConfigurationElement as each children. There is also ConfigurationElementCollection for Collections. An example from the .net Framework: <roleManager> is a Section, <providers> is an ElementCollection. I blogged about my solution.

Original Question: I have a custom sectionGroup in my web.config:

<sectionGroup name="myApp" type="MyApp.MyAppSectionGroup">
  <section name="localeSettings" 
           type="MyApp.MyAppLocaleSettingsSection"/>
</sectionGroup>

The sectionGroup itself is supposed to have an attribute:

<myApp defaultModule="MyApp.MyAppTestNinjectModule">
  <localeSettings longDateFormat="MM/dd/yyyy HH:mm:ss" />
</myApp>

I have trouble accessing that attribute (defaultModule). Getting a section is very easy using ConfigurationManager.GetSection("myApp/localeSettings") and casting it to a class that inherits from ConfigurationSection.

But I can’t seem to easily access the sectionGroup, as ConfigurationManager.GetSection("myApp") returns null. I’ve tried ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["myApp"], but this does not implement an indexer that gives me access to defaultModule.

Am I misunderstanding something? Are sectionGroups really only Containers with no settings of their own? Can I nest sections without having a sectionGroup at all? And is OpenExeConfiguration appropriate for .net applications (with web.config) or is it just for app.config’s?

Edit: Thanks for the hint regarding WebConfigurationManager instead of ConfigurationManager. That doesn’t solve my main problem, but at least this has more sensible named OpenXXX methods.

At the moment, these are the two classes. The LocaleSettings works perfectly fine, just the SectionHandler doesn’t let my access the “defaultModule” attribute.

public class MyAppSectionGroup: ConfigurationSectionGroup
{
    [ConfigurationProperty("localeSettings")]
    public MyAppLocaleSettingsSection LocaleSettings
    {
        get
        {
            return Sections["localeSettings"] as MyAppLocaleSettingsSection;
        }
    }
}

public class MyAppLocaleSettingsSection: ConfigurationSection
{
    [ConfigurationProperty("longDateFormat", DefaultValue = "yyyy-MM-dd HH:mm")]
    public string LongDateFormat
    {
        get
        {
            return this["longDateFormat"] as string;
        }
    }
}

 

 

————————————————-

Answer

Almost all you’re trying to do should be possible and should be ok and legal – you must be missing something small, I guess. The only thing I’m not sure of is whether section groups can have attributes of their own – they might be designed as being just containers for sections, which then have the actual config data in them…

For accessing the web.config, you should also try to use the WebConfigurationManager instead of the “straight” ConfigurationManager (that’s for app.config files).

Can you show us the code for your MyApp.MyAppSectionHandler and MyApp.MyAppLocaleSettingsConfigurationSection ?

Have you checked out Jon Rista’s three-part series on .NET 2.0 configuration up on CodeProject? It’s an excellent intro to how to use and extend the .NET config system – highly recommended, and most useful indeed!

If you’re dealing with custom config sections, I would also recommend you have a look at the Configuration Section Designer, a Visual Studio plug-in that allows you to visually define your config section groups and config sections and the attributes and their datatypes in those sections – great time saver and educational tool!

Some more digging has shown:

  • you could potentially define [ConfigurationProperty] on a custom config section group, but there is no automatic “backing store” for those properties, and I can’t see any way of hooking into the loading of the XML from the config file, either – so I guess the section groups really are only containers
  • you can nest section groups within one another, but the leaf level has to be a configuration section, and those cannot be nested in anything but a config section group.

Marc

.net,asp.net,web-config

Leave a Reply

Your email address will not be published. Required fields are marked *