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!