Differences in behavior between System.Web.Configuration.WebConfigurationManager and System.Configuration.ConfigurationManager
Asked Answered
R

2

12

I had some trouble on a test server with an ASP.NET website. I goofed, and had the home directory of the Default Web Site pointed to the wrong place. When I tried:

ConfigurationManager.ConnectionStrings["connectionString"]; 

it returned null, but

using System.Web.Configuration;

/* ... */

var rootWebConfig =
    WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

rootWebConfig.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;` 

returned the correct connection string.

What are all the differences between the two approaches?

EDIT: What I'm really asking is, why does the ConfigurationManager approach fail when the home directory is incorrectly set, but succeeds otherwise, and the WebConfigurationManager succeeds regardless of whether the home directory is correctly set? Are there any other differences, such as assumptions about access control?

Roof answered 13/1, 2010 at 23:6 Comment(0)
O
32

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
Omniscient answered 13/1, 2010 at 23:6 Comment(7)
Great answer so far. Why is it that the ConfigurationManager didn't find the web.config when the home directory was incorrect, whereas the WebConfigurationManager did? Does the ConfigurationManager start with the base web.config, then drill in, whereas the WebConfigurationManager goes straight to the virtual directory's web.config?Roof
I completely didn't answer your question and then added an edit to the top.Omniscient
I added information about how I reveal what directory and config file ConfigurationManager is trying to access, with results from my machine. Can you repeat on yours and see where your config file is coming from ...Omniscient
On a brand new web application project with one connection string, WebConfigurationManager.ConnectionStrings.ElementInformation.Source returns null, WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath).ConnectionStrings.ElementInformation.Source returns "C:\\Users\\David\\Documents\\Visual Studio 2008\\Projects\\WebConfigTests\\WebConfigTests\\web.config"; and ConfigurationManager.ConnectionStrings.ElementInformation.Source returns null even though ConfigurationManager.ConnectionStrings.Count is 2?Roof
What does this return for Source? ((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformationOmniscient
I'm using that particular version because it tests the default init of ConfigurationManager instead of web manager. And it was the ConfigurationManager that originally gave you null connection string.Omniscient
((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformation.Source returns "C:\\Documents and Settings\\David\\My Documents\\Visual Studio 2008\\Projects\\WebConfigTests\\WebConfigTests\\web.config"Roof
C
1

First class provides access to general client config files (such as app.config) and second one for web application's files (such as web.config).

Coarsegrained answered 13/1, 2010 at 23:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.