Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Backwards compatibility of System.Configuration.Configuration?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 27 Sep 2007 14:04 GMT
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
Linda Liu [MSFT] - 28 Sep 2007 10:12 GMT
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.
Mark - 28 Sep 2007 13:44 GMT
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,
>
[quoted text clipped - 42 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeffrey Tan[MSFT] - 02 Oct 2007 07:31 GMT
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.
Linda Liu [MSFT] - 05 Oct 2007 04:47 GMT
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
Linda Liu[MSFT] - 09 Oct 2007 12:19 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.