.NET Framework - Backwards compatibility of System.Configuration.Configuration?

Asked By mmodral on 27-Sep-07 09:04 AM
Hi...

I've got an old app config that I'm trying to work with using the new 2.0
config framework, e.g.
type="System.Configuration.NameValueFileSectionHandler"/>


If I parse this config with
ConfigurationManager.OpenMappedExeConfiguration() or OpenExeConfiguration()
and then execute
config.GetSection("Test");
I get back a System.Configuration.DefaultSection.

My problem is that I can't seem to do much with that.
SectionInformation.Type = "System.Configuration.NameValueSectionHandler",
but SectionInformation.ConfigSource = "" and
ElementInformation.IsCollection = false
ElementInformation.Source = null

I found some articles/examples of how to code new section handlers to the
ConfigurationSection/ConfigurationElement model, but is there any way to get
backwards compatibility to old handlers?  Am I missing something or is this
just not a bridge I can cross?

Thanks
Mark




v-lli replied on 28-Sep-07 05:12 AM
Hi Mark,

You should use the static method GetSection of the ConfigurationManager
class to get your section. In your practice, the returned section is of the
type NameValueCollection.

The following is a sample:

using System.Configuration;
using System.Collections.Specialized;

private void button1_Click(object sender, EventArgs e)
{
NameValueCollection section =
ConfigurationManager.GetSection("Test") as NameValueCollection;
// get the value with the key "test" in the collection
string value = section["test"];

}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
mmodral replied on 28-Sep-07 08:44 AM
Hi Linda...

Actually, this completely avoided the question.

My understanding was that the new apis offered by
System.Configuration.Configuration and
ConfigurationManager.OpenExeConfiguration() were to address those cases where
a program wanted to examine values in valid config file *other* than

For AppSettings and ConnectionStrings, System.Configuration.Configuration is
near enough to the behavior of ConfigurationManager that it's easy to work
with.

My question was whether System.Configuration.Configuration had any concept
of backwards compatibility on other custom sections.  I've found examples of
how to implement custom sections with the newer ConfigurationSection and
ConfigurationElement, but not anything on how to handle validly implemented
legacy sections.

DefaultSection seems to know how the custom section was implemented but I
haven't found any way to get to the detail from it.

Thanks
Mark
jeta replied on 02-Oct-07 02:31 AM
Hi Mark,

Thanks for your feedback.

We will perform some research on this issue and get back to you ASAP.
Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
v-lli replied on 04-Oct-07 11:47 PM
Hi Mark,

Sorry for the delay!

The System.Configuration.DefaultSection represents a basic
configuration-section handler that exposes the configuration section's XML
for both read and write access.

This is the type that is returned when the design-time API reads in a
section that is not registered in the <configSections> section. This type
is also returned when the type of registered section does not inherit from
ConfigurationSection, as with sections that use the .NET 1.0 or 1.1
IConfigurationSectionHandler type.

Your scenario is the latter case because the
System.Configuration.NameValueSectionHandler type inherites from the
IConfigurationSectionHandler type.

After we get the DefaultSection object, we can retrieve the raw XML within
the DefaultSection object. The following is a sample:

Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.Sections["Test"];
// in your practice, the variable 'xml' returns "<Test>\r\n    <add
key=\"test\" value=\"this\"/>\r\n  </Test>"
string xml = section.SectionInformation.GetRawXml();

As we can see, it's not convenient to get the value of the key 'test' from
the above variable 'xml'.

The following MSDN document introduces how to create custom configuration
sections using IConfigurationSectionHandler as well as how to
programmatically access the custom configuration data:
'How to: Create Custom Configuration Sections Using
IConfigurationSectionHandler'
https://msdn2.microsoft.com/en-us/library/ms228056.aspx

As you can see, the sample in the above document uses the
System.Configuration.ConfigurationSettings.GetConfig method to access the
custom configuration data. If you look up this method in MSDN, you'll find
this method is now obsolete and has been replaced by
System.Configuration.ConfigurationManager.GetSection method.

So we should use ConfigurationManager.GetSection method to configuration
sections that inherties from the IConfigurationSectionHanlder type.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
v-lli replied on 09-Oct-07 07:19 AM
Hi Mark,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
jeremy simmons replied to v-lli on 03-Feb-10 02:31 PM
Linda is correct -

you need to treat your returned ConfigurationSection as a DefaultSection instance.

This gives you enough context to get at the SectionInformation property, which contains a GetRawXml() method that returns a string.

An instance of the SingleTagSectionHandler has a method named Create which will convert the xml string to a Hashtable instance.

See my answer on stack overflow for more details.

http://stackoverflow.com/questions/1149443/namevaluesectionhandler-can-i-use-this-section-type-for-writing-back-to-the-app/2194763#2194763
S├ębastien THEROUDE replied to jeremy simmons on 31-Aug-11 05:16 AM
Hi,



I tried the solution you described but I have the following exception 'System.InvalidCastException' when I use SingleTagSectionHandler Create function.



System::Configuration::SingleTagSectionHandler^ handler = gcnew System::Configuration::SingleTagSectionHandler();

handler->Create(0, 0, readableSectionRawXmlDocument->DocumentElement);



Do you have any ideas why this exception occurs ?