How to handle ConfigurationSection containing unsupported properties in .NET 4.8?

160 Views Asked by At

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!

1

There are 1 best solutions below

1
On BEST ANSWER

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.