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