Calling Save on a Configuration obj with a custom sectiongroup of type NameValueSectionHandler

Posted by Allen on Stack Overflow See other posts from Stack Overflow or by Allen
Published on 2010-03-23T20:11:25Z Indexed on 2010/03/23 20:13 UTC
Read the original article Hit count: 445

I've got aConfiguration object that I can easily read and write settings from and call Save(ConfigurationSaveMode.Minimal, true) on and that typically works just fine.

However, I now have a config file that has a custom sectionGroup defined of type System.Configuration.NameValueSectionHandler (I didn't write that part and I can't change it). Now when I call the Save method, it throws a TypeLoadException

Message:

Type System.Configuration.NameValueSectionHandler does not inherit from System.Configuration.ConfigurationSectionGroup.

Callstack:

at System.Configuration.TypeUtil.VerifyAssignableType
      (Type baseType, Type type, Boolean throwOnError)
at System.Configuration.TypeUtil.GetConstructorWithReflectionPermission
      (Type type, Type baseType, Boolean throwOnError)
at System.Configuration.MgmtConfigurationRecord.CreateSectionGroupFactory
      (FactoryRecord factoryRecord)
at System.Configuration.MgmtConfigurationRecord.EnsureSectionGroupFactory
      (FactoryRecord factoryRecord)
at System.Configuration.MgmtConfigurationRecord.GetSectionGroup
      (String configKey)
at System.Configuration.ConfigurationSectionGroupCollection.Get
      (String name)
at System.Configuration.ConfigurationSectionGroupCollection.
      <GetEnumerator>d__0.MoveNext()
at System.Configuration.Configuration.ForceGroupsRecursive
      (ConfigurationSectionGroup group)
at System.Configuration.Configuration.SaveAsImpl
      (String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at System.Configuration.Configuration.Save
      (ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at MyCompany.MyProduct.blah.blah.Save
      () in C:\\projects\\blah\\blah\\blah.cs:line blah

For reference, the configuration goes like so

<configuration>
  <configSections>
    ... normal sections here that work just fine, followed by ...

    <sectionGroup name="threadSettings" type="System.Configuration.NameValueSectionHandler">
      <section name="T1" type="System.Configuration.DictionarySectionHandler"/>
      <section name="T2" type="System.Configuration.DictionarySectionHandler"/>
      <section name="T3" type="System.Configuration.DictionarySectionHandler"/>
    </sectionGroup>
  </configSections>
  <applicationSettings />
  <threadSettings>
    <T1>
      <add key="key-here" value="value-here"/>
    </T1>
    ... T2 and T3 here as well, thats not interesting ...
  </threadSettings>
</configuration>

If I remove the actual sections and the definition for the custom section groups, I am able to read / write settings just fine and even call Save just fine.

Obviously the custom section groups could be setup a little nicer but I can't fix that so I'm wondering: Is it possible to get the Configuration object to save if it has custom sectionGroups of the NameValueSectionHandler type

© Stack Overflow or respective owner

Related posts about c#

Related posts about configuration