Backwards compatibility of System.Configuration.Configuration?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi...

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

<Test>
<add key="test" value="this"/>
</Test>
</configuration>

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
 
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.
 
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
<myapp>.exe.config.

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


Linda Liu said:
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.
 
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.
 
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
 
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
 
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/...e-for-writing-back-to-the-app/2194763#2194763



v-lli wrote:

Hi Mark,Sorry for the delay!
04-Oct-07

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.asp

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 Li
Microsoft Online Community Support

Previous Posts In This Thread:

Backwards compatibility of System.Configuration.Configuration?
Hi..

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

<Test
<add key="test" value="this"/
</Test
</configuration

If I parse this config with
ConfigurationManager.OpenMappedExeConfiguration() or OpenExeConfiguration()
and then execut
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 = "" an
ElementInformation.IsCollection = fals
ElementInformation.Source = nul

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

Thank
Mark

Hi Mark,You should use the static method GetSection of the
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.

Hi Linda...Actually, this completely avoided the question.
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
<myapp>.exe.config.

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


:

Hi Mark,Thanks for your feedback.
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.

Hi Mark,Sorry for the delay!
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

Hi Mark,How about the problem now?
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


Submitted via EggHeadCafe - Software Developer Portal of Choice
Silverlight 2 Beta 2 - Doing Data Part I
http://www.eggheadcafe.com/tutorial...cc-58558a7e83f8/silverlight-2-beta-2--do.aspx
 
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/...e-for-writing-back-to-the-app/2194763#2194763


Not only was Linda correct, she was more timely! 2 Oct 2007!
 
Back
Top