Web.Config : Configuration by Environment

7 10 2008

During a web project lifecycle, we encounter always the same problem when deploiement is required in development environment(Server A + DB A), Receipt/Qualif (Server B + DB B) and production (Server C + DB C without taking care of web farming). Many developpers choose to create one web.config for each environment; not a bad choice but a permanent synchro is required when adding reference or others commons configurations.
I was looking to improve this system to minimize the synchro time between each web.config file and find a simple tips to exclude the specific data from a web.config : the configSource property.

This way, it’s possible to write a web.config like that :

<?xml version=1.0?>

<configuration>

<configSections>

<sectionGroup name=system.web.extensions type=System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35>

<sectionGroup name=scripting type=System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35>

<section name=scriptResourceHandler type=System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 requirePermission=false allowDefinition=MachineToApplication/>

<sectionGroup name=webServices type=System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35>

<section name=jsonSerialization type=System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 requirePermission=false allowDefinition=Everywhere/>

<section name=profileService type=System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 requirePermission=false allowDefinition=MachineToApplication/>

<section name=authenticationService type=System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 requirePermission=false allowDefinition=MachineToApplication/>

<section name=roleService type=System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 requirePermission=false allowDefinition=MachineToApplication/>

</sectionGroup>

</sectionGroup>

</sectionGroup>

</configSections>

<appSettings configSource=Config\ApplicationSettings.Config/>

<connectionStrings configSource=Config\ConnectionStrings.Config/>

and simply create a file named ApplicationSettings.Config(the name can be changed) in my folder Config. This one contains :

<?xml version=1.0?>

<appSettings>

<!– DEV –>

<add key=Key1 value=Value1 for Environment 1/>

Thus we only need to create for each environnement a file named ApplicationSettings.Config with the specific parameters. It reduces the number of parameters to the minimum.
When deploying, you should only copy the web.config and the appropriated ApplicationSettings.Config.

In my example, we can see an other configSource for connectionString based on the same principle. The connectionString section is now describe in another file :

<?xml version=1.0?>

<connectionStrings>

<add name=DB1 connectionString=Application Name=APP1;Data Source=DB1;Initial Catalog=APP1_Dev;Persist Security Info=True; providerName=System.Data.SqlClient/>

</connectionStrings>

Advertisements

Actions

Information

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s




%d bloggers like this: