What's the Difference Between the Webconfigurationmanager and the Configurationmanager

What's the difference between the WebConfigurationManager and the ConfigurationManager?

WebConfigurationManger knows how to deal with configuration inheritance within a web application. As you know, there could be several web.config files in one applicaion - one in the root of the site and any number in subdirectories. You can pass path to the GetSection() method to get possible overridden config.

If we'd looke at WebConfigurationManager with Reflector then things are clear:

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

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

Differences in behavior between System.Web.Configuration.WebConfigurationManager and System.Configuration.ConfigurationManager

Try This:

Put a breakpoint where your ConfigurationManager statement is and run the following in the Immediate Output window
((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformation
. My machine reports Source: "C:\Users\John\Documents\Visual Studio 2008\Projects\StackOverflowCode\WebApplication1\web.config" as seen below.

Note: The following also shows mine is accessing the ASP.NET web.config.

{System.Configuration.ElementInformation}
Errors: {System.Configuration.ConfigurationException[0]}
IsCollection: true
IsLocked: false
IsPresent: true
LineNumber: 17
Properties: {System.Configuration.PropertyInformationCollection}
Source: "C:\\Users\\John\\Documents\\Visual Studio 2008\\Projects\\StackOverflowCode\\WebApplication1\\web.config"
Type: {Name = "ConnectionStringsSection" FullName = "System.Configuration.ConnectionStringsSection"}
Validator: {System.Configuration.DefaultValidator}

And when I run ConfigurationManager.ConnectionStrings.ElementInformation I get Source:null which is correct for my web app.

What do you get for a configuration Source path???


Immediate Assumption

ConfigurationManager.ConnectionStrings["connectionString"]; might look for a config location which isn't necessarily the same as the web application's root web.config. Likely it's looking in a Windows directory (e.g at a different place or for machine.config). Trying to find an appropriate test for this though.


Intentions of Each Manager

System.Configuration.ConfigurationManager can access the .NET configuration XML format which means it reads both:

  • web configurations (i.e. web.config file in ASP.NET)
  • and non-web configurations (e.g. app.config file - standalone console app, Windows app, etc.)

and expresses those aspects that are common to types of configuration. This is a general purpose config manager. (However despite this ability to look at both types of configs, you should use it for app configs because the web manager is devoted to the web configs, as described next ...)

System.Web.Configuration.WebConfigurationManager does pretty much the same thing but is the "webified" version of configuration manager, providing access to ASP.NET web-specific aspects of configuration (e.g. web.config file in ASP.NET).

Similarities

See member similarities between ConfigurationManager.* and WebConfigurationManager.*

Both managers can, for example, access an AppSettings property and a ConnectionStrings property. Indeed both these settings are common to both kinds of config and are even located at the same level in the XML document.

So there are many similarities however,

Behavioral Differences

Accessing configuration:
ConfigurationManager has methods to open standalone app configs (i.e. Myprogram.EXE's App.config) relative to the EXEC app, whereas WebConfigurationManager has methods to open the ASP.NET web config relative to it's web application root directory in the web site.

Here's a basic app.config (e.g. opened via "C:\winapp\app.config" from a disk folder by ConfigurationManager)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings/>
<connectionStrings/>
</configuration>

And here's a basic web.config (e.g. opened via "~" meaning root of web site by WebConfigurationManager)

<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>

<system.web>
<!-- special web settings -->
</system.web>

</configuration>

Notice the similarities. Also notice the web configuration has an additional system.web element for ASP.NET.

These managers are located in different assemblies.

  • ConfigurationManager: System.Configuration.dll
  • WebConfigurationManager: System.Web.dll

CloudConfigurationManager vs WebConfigurationManager?

CloudConfigurationManager enables us to read configuration file regardless of the environment we are in.

So instead of writing environment specific code statements e.g., for web.config file:

WebConfigurationManager.AppSettings["MySetting"]

For ServiceConfiguration.cscfg file:

RoleEnvironment.GetConfigurationSettingValue("MySetting")

We can write the below statement, which will read values from all the configuration files i.e., app.config, web.config and ServiceConfiguration.cscfg.

CloudConfigurationManager.GetSetting("MySetting")

Using ConfigurationManager in web apps

MSDN says that WebConfigurationManager is the preferred way to work with configuration files in web applications.

In web applications, configuration can be inherited from a web.config file in a parent virtual directory: WebConfigurationManager will handle such inherited settings properly, which I believe isn't the case for ConfigurationManager.

Of course, if your application does not inherit such settings (e.g. is always at the root of a web site), this is not relevant to you.

Is ConfigurationManager.AppSettings available in .NET Core 2.0?

Yes, ConfigurationManager.AppSettings is available in .NET Core 2.0 after referencing NuGet package System.Configuration.ConfigurationManager.

Credits goes to @JeroenMostert for giving me the solution.

Using ConfigurationManager in web apps

MSDN says that WebConfigurationManager is the preferred way to work with configuration files in web applications.

In web applications, configuration can be inherited from a web.config file in a parent virtual directory: WebConfigurationManager will handle such inherited settings properly, which I believe isn't the case for ConfigurationManager.

Of course, if your application does not inherit such settings (e.g. is always at the root of a web site), this is not relevant to you.

.Net Configuration files confusion

This is actually default behavior - ConfigurationManager.AppSettings read the current web.config for all DLLs.

Sometimes .config files are created for other projects (or dlls), but they are not used in the site and should be merged into web.config.



Related Topics



Leave a reply



Submit