0

How can I load a ConfigurationSection when the config file contains properties that are no longer supported?

If I version a System.Configuration.ConfigurationSection by removing a ConfigurationProperty that is no longer supported by my product, is there a way for customers to still load their files without removing the property I removed?

For example, if I currently have a ConfigurationSection like this:

public class CustomConfigSection : ConfigurationSection
{
    [ConfigurationProperty("supportedFeatureConfiguration")]
    public SupportedFeatureConfigElement SupportedFeatureConfig => (SupportedFeatureConfigElement)base["supportedFeatureConfiguration"];
    
    [ConfigurationProperty("unsupportedFeatureConfiguration")]
    public UnsupportedFeatureConfigElement UnsupportedFeatureConfig => (UnsupportedFeatureConfigElement)base["unsupportedFeatureConfiguration"];
}

and I version to this:

public class CustomConfigSection : ConfigurationSection
{
    [ConfigurationProperty("supportedFeatureConfiguration")]
    public SupportedFeatureConfigElement SupportedFeatureConfig => (SupportedFeatureConfigElement)base["supportedFeatureConfiguration"];
}

Could I change how I load the XML to let my product accept customer configs with <unsupportedFeatureConfiguration> properties?

Currently I use a method like this to load in config files:

private void DummyCode()
{
    var configuartion = ConfigurationManager.OpenExeConfiguration("My Config Path");
    var mySection = configuartion.Sections.Cast<ConfigurationSection>().FirstOrDefault(c => c.SectionInformation.Name == "My Section Name");

    if (mySection == null)
    {
        return;
    }

    using (var fs = new FileStream("My File Name", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        var xmlDoc = XDocument.Load(fs);
        mySection.SectionInformation.SetRawXml(xmlDoc.ToString());
    }
}

I tried just removing the entries from my ConfigurationSection but that throws an unrecognized error exception.

I think I may have to fallback to marking the properties as [Obsolete] and leaving them in the ConfigurationSection - but a tidier solution would be greatly appreciated.

There are many varied users, so breaking changes must be avoided (anything that means they'd all need to edit their .config files).

Any help greatly appreciated, SO!

r_h_w
  • 13
  • 4
  • Your configuration file and c# code must be consistent. If you do not use a property in the configuration file, there is nothing wrong with leaving the property in the configuration file. But if you c# code requires a property than it must be in the configuration file. If you are removing a section in the configuration file and getting an exception the c# code must be using the property. – jdweng May 19 '23 at 15:19
  • @jdweng hey, thanks for the reply. I may not have been clear - the exception I get is because I removed a property from my C# class that represents .config (xml) file, but the property is still in the live .config (xml) files. The C# code is definitely not using the property. – r_h_w May 23 '23 at 14:25
  • Which line is giving the exception? Reading the xml file should not give an exception. if you are deserializing the xml a property in the xml will be ignored if there is no class. So someplace in the code you are actually are trying to use the property. – jdweng May 23 '23 at 14:28
  • @jdweng thank you for your reply. The exception comes on mySection.SectionInformation.SetRawXml(xmlDoc.ToString()); The XML gives an exception because as described, the xml file (.config for me) contains an tag, but the CustomConfigSection class no longer contains that tag. So I can guarantee 100% that the code is no longer using that tag, because it has been removed. The problem is it still appears in the xmlDoc. – r_h_w May 24 '23 at 15:12

1 Answers1

0

If you want to handle exists/not-exists scenarios in the xml.. you could actually "go back in time" and do custom configuration as it was in DotNet Framework 1.1 (before 2.0)....before 2.0 "cleaned up and gave cleaner ways" to do custom configuration.

See: https://learn.microsoft.com/en-us/previous-versions/aspnet/ms228056(v=vs.100)

IConfigurationSectionHandler is the key interface.

But basically...."you get the xml" and you can code as little or as much voodoo as you want against that xml to create your custom "strongly typed poco". It is very "dom'ish" to find things.

Now, I don't know if they have brought that interface all the way forward with dotnet framework 4.x. I know you could still use it with 2.0 and I think 3.5.

This below article is C# and a little easier to follow than the MS example above.

https://www.webtrainingroom.com/aspnetmvc/custom-configuration-sections

public class SystemSettingsHandler : IConfigurationSectionHandler            
{
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
    {

The key point is the code above executes.. and using "XmlNode section".. you can do as little or as much with the dom elements.

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • Thank you for your response. I've just tested out creating my own 'IConfigurationSectionHandler' and believe it works - I'm just weighing up if I want to add that code to my XML serialization. I think this counts as the correct answer for my problem though, cheers! – r_h_w May 23 '23 at 15:08