ConfigurationManager vs WebConfigurationManager vs CloudConfigurationManager

In my recent post, I have explained various ways of storing application settings and connection strings for the web application hosted in Azure App Service. Let’s now see what .NET Framework provides us to consume these settings from the code and when to use what?.

ConfigurationManager:

Namespace: System.Configuration
Assembly: System.Configuration.dll

Provides access to configuration files for client applications. This class cannot be inherited. – MSDN

Although we can use ConfigurationManager class anywhere we have XML based configuration file such as app.config or web.config, it is mainly intended for client applications such as console app, windows app etc.,

You can get any of the sections from configuration file using its method “GetSection”. It also provides direct properties of type NameValueCollection for sections like AppSettings and ConnectionStrings.


WebConfigurationManager: (vs ConfigurationManager)

Namespace: System.Web.Configuration
Assembly: System.Web

Provides access to configuration files as they apply to Web applications. – MSDN

This is pretty much similar to ConfigurationManager in terms of accessing common settings such as AppSettings or ConnectionStrings from XML based configuration file(app.config or web.config) but the actual difference comes in the behavior while accessing the configuration files.

With ConfigurationManager, we have methods to open Configuration file (app.config) of any client application (console. windows app etc.,) relative to its EXE. Whereas with WebConfigurationManager, it has methods to open Configuration file (web.config) relative to its website’s (ASP.NET) root directory.

If you have a web application such as ASP.NET, It is recommended use WebConfigurationManager which is a web version of ConfigurationManager designed specially for web applications and provides methods for web-specific aspects of configuration.

One such aspect is that handling of Configuration Inheritance within a web application. As we know, there could be several configuration files (web.config) in a application like one at the root of website and it can contain one in each of its sub-directories. In such case, WebConfigurationManager has a overloading method for “GetSection” which takes path of the configuration relative to root directory.

If you look inside System.Web.Configuration, it uses ConfigurationManager itself for most of the methods except those doesn’t have equivalent method (ex: GetSection(string, string) ).

using System.Configuration;
...
namespace System.Web.Configuration
{
  public static class WebConfigurationManager
  {
    public static NameValueCollection AppSettings
    {
      get
      {
        return ConfigurationManager.AppSettings;
      }
    }

    public static ConnectionStringSettingsCollection ConnectionStrings
    {
      get
      {
        return ConfigurationManager.ConnectionStrings;
      }
    }

    public static object GetSection(string sectionName)
    {
      ...
      return ConfigurationManager.GetSection(sectionName);
    }

    public static object GetSection(string sectionName, string path)
    {
      ...
      return HttpConfigurationSystem.GetSection(sectionName, path);
    }

    public static object GetWebApplicationSection(string sectionName)
    {
      ...
      return ConfigurationManager.GetSection(sectionName);
    }
  }
}


CloudConfigurationManager: (vs WebConfigurationManager)

Namespace: Microsoft.Azure
Assembly:  Microsoft.WindowsAzure.Configuration

Configuration manager for accessing Microsoft Azure settings. – MSDN

It would be part of the Azure SDK installed or you can import Nuget Package to get the reference to CloudConfigurationManager.

Unlike, ConfigurationManager and WebConfigurationManager, it just supports AppSettings of either of app.config or web.config files.

CloudConfigurationManager.GetSetting("Property")

If your web application is hosted on Azure, then you can use CloudConfigurationManager instead of WebConfigurationManager to access AppSettings ether from web.config file of the application or settings provided in Azure App Settings (Portal).

You could also use CloudConfigurationManager to access settings from Cloud Service Configuration file (*.cscfg) file as shown here below.

Instead of

RoleEnvironment.GetConfigurationSettingValue("Key")

You can use

CloudConfigurationManager.GetSetting("Key")

Simillar to WebConfigurationManager, even CloudConfigurationManager also uses ConfigurationManager class internally to retrieve AppSettings

internal string GetSetting(string name)
    {
        ...
        value = GetValue("ConfigurationManager", name, n => ConfigurationManager.AppSettings[n]);

        return value;
    }

That’s all about Configuration Managers we have. Hope it helps!!!

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s