Distributed ConnectionString in ASP.NET


Introduction

In this post you are going to take a look at Distributed ConnectionString in ASP.NET. We will store our configuration settings in separate configuration file (that's I am calling distributed).

Managing huge list of ConnectionStrings in one configuration file (Web.config) is one of the difficult jobs in ASP.NET based web designing. So, I am writing this video post for you that will light a way to deal.

Also, Web.config file has much information already so placing connection strings in that creates management difficulties if you work on big projects.

What I am going to do?

I have a Web.config file in my project that has following information (including connection strings):

<?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\vx.x\Config
-->
<
configuration>

  <connectionStrings>
    <
add name="ConnStr1" connectionString="infos***"/>
    <add name="ConnStr2" connectionString="infos***"/>
    <add name="ConnStr3" connectionString="infos***"/>
    <add name="ConnStr4" connectionString="infos***"/>
    <add name="ConnStr5" connectionString="infos***"/>
    <add name="ConnStr6" connectionString="infos***"/>
  </connectionStrings>

       <system.web>
              <!--

            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
       
-->
              <
compilation debug="true">
                     <assemblies>
                           <
add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                           <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                           <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                           <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                     </assemblies>
              </
compilation>
              <!--
          The <authentication> section enables configuration
          of the security authentication mode used by
          ASP.NET to identify an incoming user.
       
-->
              <
authentication mode="Windows"/>
              <!--
           The <customErrors> section enables configuration
           of what to do if/when an unhandled error occurs
           during the execution of a request. Specifically,
           it enables developers to configure html error pages
           to be displayed in place of a error stack trace.

           <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
             <error statusCode="403" redirect="NoAccess.htm" />
             <error statusCode="404" redirect="FileNotFound.htm" />
           </customErrors>
       
-->
              <
pages>
                     <
controls>
                           <
add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                           <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                     </controls>
              </
pages>
              <
httpHandlers>
                     <
remove verb="*" path="*.asmx"/>
                     <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                     <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                     <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
              </httpHandlers>
              <
httpModules>
                     <
add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
              </httpModules>

       </system.web>
       <
system.codedom>
              <
compilers>
                     <
compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                           <providerOption name="CompilerVersion" value="v3.5"/>
                           <providerOption name="WarnAsError" value="false"/>
                     </compiler>
                     <
compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                           <providerOption name="CompilerVersion" value="v3.5"/>
                           <providerOption name="OptionInfer" value="true"/>
                           <providerOption name="WarnAsError" value="false"/>
                     </compiler>
              </
compilers>
       </
system.codedom>
       <!--

        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
   
-->
       <
system.webServer>
              <
validation validateIntegratedModeConfiguration="false"/>
              <modules>
                     <
remove name="ScriptModule"/>
                     <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
              </modules>
              <
handlers>
                     <
remove name="WebServiceHandlerFactory-Integrated"/>
                     <remove name="ScriptHandlerFactory"/>
                     <remove name="ScriptHandlerFactoryAppServices"/>
                     <remove name="ScriptResource"/>
                     <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                     <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                     <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
              </handlers>
       </
system.webServer>
       <
runtime>
              <
assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
                     <dependentAssembly>
                           <
assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
                           <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
                     </dependentAssembly>
                     <
dependentAssembly>
                           <
assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
                           <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
                     </dependentAssembly>
              </
assemblyBinding>
       </
runtime>
</
configuration>

In above configuration file, our one of the very important information that is connection string is hidden in other codes, so let's separate it.

Create a new configuration file and delete all information appeared by default in that file and cut the following information from above file and add it in new configuration file:

  <connectionStrings>
    <
add name="ConnStr1" connectionString="infos***"/>
    <add name="ConnStr2" connectionString="infos***"/>
    <add name="ConnStr3" connectionString="infos***"/>
    <add name="ConnStr4" connectionString="infos***"/>
    <add name="ConnStr5" connectionString="infos***"/>
    <add name="ConnStr6" connectionString="infos***"/>
  </connectionStrings>

Now in the source file (Web.config), we need to add the reference of the new configuration file in our Web.config file, for this add following code in Web.config file at the exact location where we cut.

<connectionStrings configSource="new config file name" />

Now, run the project and see the magic.

Video Post

Untitled.jpg
 

Recommended Ebook

Diving Into ASP.NET WebAPI

Download Now!
Similar Articles