Caveman's Blog

My commitment to learning.

Factoring the Web.Config

with one comment


Web.Config is a XML document file in a ASP.Net application that is used to store configuration settings related to the web application. The web.config file contains information about database configuration, control module loading, security configuration, session state configuration, Custom configuration and compilation settings. Each web application in ASP.Net inherits their base web.config from the machine’s web.config located in %SystemRoot%\Microsoft.Net\Framework\v#.#.#.#\CONFIG. The applications own web.config is physically located at the root of the web application and sub directories inherit the configuration setting unless they have their own web.config with in the respective sub-directories.

Usually as the application size grows, so does the number of settings and the size of the web.config file. The settings in config file become so many that it will be pretty tough to manage them and also hard to read. There is one less known optional string attribute called “ConfigSource” that specifies the name of the include file in which the associated configuration section is defined, if such a file exists. The configsource attribute was introduced in .NET Framework 2.0 to support external configuration files. This attribute can be added to any configuration section to specify a an external file for that section. Following is an example of a before and after scenario for using the configsource attribute. This example demonstrates how the database connection settings can be separated into an external config file:

Before:

<configuration>
    <configSections>
        <section name="ConnectionStrings" restartOnExternalChanges="false"/>
    </configSections>
    <connectionStrings>
        <add name=“Inventory” connectionString=“Database=MYINVENTORYDB;Server=<SERVER-NAME>;User ID=<USERNAME>;Password=<PASSWORD>;Trusted_Connection=False;” providerName=“System.Data.SqlClient”/>
    </connectionStrings>
.
.
<configuration>

After:

Following are two sections from the web.config and the dbsetting.config files. The dbsettings.config file has the database connection configuration information.

web.config

<configuration>
    <configSections>
        <section name="ConnectionStrings" restartOnExternalChanges="false"/>
    </configSections>
    <connectionStrings configSource="dbsettings.config">

      </connectionStrings>
.
.
<configuration>

dbsettings.config

<configuration>
    <connectionStrings>
        <add name=“Inventory” connectionString=“Database=MYINVENTORYDB;Server=<SERVER-NAME>;User ID=<USERNAME>;Password=<PASSWORD>;Trusted_Connection=False;” providerName=“System.Data.SqlClient”/>
    </connectionStrings>
</configuration>

Example 2

Here is another example of separating the database configuration settings into multiple files. These settings are for Enterprise Library External file configuration source for Application blocks. Following are the sections of the web.config and the entlib.config files into which the database Connection string configuration has been separated:

Web.Config

<enterpriseLibrary.ConfigurationSource selectedSource=“File Configuration Source”>
    <sources>
        <add name=“File Configuration Source” type=“Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=X.X.X.X, Culture=neutral, PublicKeyToken=null” filePath=“entlib.config”/>
    </sources>
</enterpriseLibrary.ConfigurationSource>

entlib.config

<configuration>
    <connectionStrings>
        <add name=“Inventory”connectionString=“Database=MYINVENTORYDB;Server=<SERVER-NAME>;User  ID=<USERNAME>;Password=<PASSWORD>;Trusted_Connection=False;” providerName=“System.Data.SqlClient”/>
    </connectionStrings>
</configuration>

The end result is that this feature of web.config file has been very useful to me so far in keeping the config files neat and simple to read. Also note that the restartOnExternalChanges property is set to false in the “section element for config sections” so that the application does not restart when a change to the external config file is made.

Tip: ConnectionStrings.com has nice list of connection strings for various databases.

References:

1. MSDN Online

Written by cavemansblog

June 4, 2009 at 2:39 pm

One Response

Subscribe to comments with RSS.

  1. Dear Friends,

    I hope you are doing well. I have launched a web site http://www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter. Looking forward to hearing from you and hope you will join with us soon.

    Please forward this email to all of your friends who are related IT. Send to us your feed about site also.

    Thank you
    RRaveen
    Founder CodeGain.com

    RRaveen

    July 2, 2009 at 9:31 am


Leave a comment