The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,057 other followers

.NET/C#: Way overdue post – the syntax inside the Obsolete attribute; Looking for the new ConfigurationManager? (via: @ScottCate Blog Moved ….)

Posted by jpluimers on 2012/04/18

Everytime I get a warning like

Warning 1 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by ConfigurationManager.AppSettings'

it reminds me I should have written a blog post about it, as the solution is a tiny bit more than just replacing System.Configuration.ConfigurationSettings.AppSettings by System.Configuration.ConfigurationManager.AppSettings.

Scott Gate wrote a nice post on his old blog about this (his new blog is awesome, he really should import his old posts into his new blog) that explains how to solve this well, so below is an elaboration on the how of the change, a tiny trick and a short series of steps to resolve this warning.

First of all, the above message means you are touching code that has been written in the .NET 1.x era, and the maintaining people (you! <g>) have been too lazy to solve the warning. That is bad, as your code should compile without warnings, and preferably without hints too.

How

First a bit of how the System.Configuration.ConfigurationSettings class looks in Assembly System.dll, v4.0.30319 (basically this has been the same from v2.0 onward):

using System;
using System.Collections.Specialized;

namespace System.Configuration
{
    // Summary:
    //     Provides runtime versions 1.0 and 1.1 support for reading configuration sections
    //     and common configuration settings.
    public sealed class ConfigurationSettings
    {
        // ...
        [Obsolete("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings")]
        public static NameValueCollection AppSettings { get; }

        // ...
        [Obsolete("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]
        public static object GetConfig(string sectionName);
    }
}

Basically it contains an AppSettings property (of type NameValueCollection) and a GetConfig methor (returning type object which actually is a ConfigurationSection instance) both marked with an Obsolete attribute.

Trick

The trick is the Message passed to the ObsoleteAttribute constructor used for those Obsolete attributes: there is a naming convention of form AssemblyName!Namespace.Class.Member for replacements.

[Obsolete("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]

So the System.Configuration in front of the exclamation mark, does not refer to one of the System.Configuration namespaces, but actually refers to the System.Configuration assembly in System.Configuration.dll which you need to add as a reference to your project.

This is one of the things that people developing .NET software easily get wrong: the confusion that the same names are used for both namespaces and assemblies (and often more than one assembly contributes to the same namespace, this complicates the namespace/assembly name matrix).

Note that by default the reference to System.Configuration assembly is not added to new projects, but you can add a custom project template to accommodate for that.

Resolve

So these are the steps to follow in order to resolve this warning:

  1. Add the System.Configuration assembly as a reference to your project
  2. Replace code like
    object config = System.Configuration.ConfigurationSettings.GetConfig(sectionName);
    with code like
    object config = System.Configuration.ConfigurationManager.GetSection(sectionName);
  3. Replace code like
    object value = System.Configuration.ConfigurationSettings.AppSettings[key];
    with code like
    object value = System.Configuration.ConfigurationManager.AppSettings[key];

Note  the AppSettings property name still being the same, but the GetConfig/GetSection method name change (GetSection still returns a ConfigurationSection instance like GetConfig does).

Reason and further reading

The reason of the whole change is that ConfigurationManager has more properties and methods than ConfigurationSettings had:

namespace System.Configuration
{
    public static class ConfigurationManager
    {
        public static NameValueCollection AppSettings { get; }
        public static ConnectionStringSettingsCollection ConnectionStrings { get; }
        public static object GetSection(string sectionName);
        public static Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel);
        public static Configuration OpenExeConfiguration(string exePath);
        public static Configuration OpenMachineConfiguration();
        public static Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel);
        public static Configuration OpenMappedMachineConfiguration(ConfigurationFileMap fileMap);
        public static void RefreshSection(string sectionName);
    }
}

Now you have the extra ConnectionStrings property of type ConnectionStringSettingsCollection and various methods to open a ConfigurationOpenExeConfiguration from either a userLevel of type ConfigurationUserLevel or an exePathOpenMachineConfigurationOpenMappedExeConfiguration and OpenMappedMachineConfiguration (with an ExeConfigurationFileMap  and ConfigurationFileMap).

There is a great question on StackOverflow on opening configurations where the accepted answer points you to all the “new” ways to open configuration information and how the .NET framework can merge configurations of different levels for you.

Like I wrote earlier: way overdue, but glad I finally posted this :)

–jeroen

via: Looking for the new ConfigurationManager? – Blog Moved …..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: