Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing complex Web.Config files between deployment environments

Does anyone know of any good tools/utilities for managing Web.Config files between different build/deployment environments?

For example, I have a WCF project that in development I don't want to enable SSL, but I do want it enabled in production. I want different logging settings, different DB connection strings, different error handling, different file paths... Even some different Unity framework bindings (wire up mocks for unit testing instead of the real objects for deployment).

Maintaining individual copies of the Web.Config is a pain, because adding a new web service means editing multiple files and keeping them in sync.

I've also noticed that if you muck with the Web.Config too much by hand, Visual Studio will choke if you try to use the "add item" wizard to, say, add a new Web Service for WCF, since it has to modify the Web.Config to add the endpoint,a nd can't parse it any more. So I have to be careful not to invalidate the existing Web.Config.

I also thought about just using some regex to do replacements and just building a new Web.Config in a pre-build command. That seems like the best option so far...

Any other ideas? It seems like this should be a very common issue, since the Web.Config should probably never be the same between development and production deployments.


Update:

I decided to write a quick console app that will take all the xml files in a given directory and merge them into one, and only include certain files based on the name.

So I can make in a directory:

WebConfig_All

<configuration>   <configSections>     ...   </configSections>   <system.web>     ...   </system.web> </configuration> 

connectionStrings_Debug

<configuration>   <connectionStrings>     <add name="connstr" connectionString="...dev..." />   </connectionStrings> </configuration> 

connectionStrings_Release

<configuration>   <connectionStrings>     <add name="connstr" connectionString="...prod..." />   </connectionStrings> </configuration> 

Then run my command line tool, and pass in the configuration (Debug, Release, custom...) And it will merge all the files that end in _All" or_<configuration>`.

So now I have 80% of my Web.Config in a single WebConfig_All file, and the 20% custom stuff in separate files per build configuration. I can then run my command line tool as a pre-build task in VisualStudio, or from NAnt, or wherever I want...

I also made my XML merge logic good enough to handle stuff like:

<x>   <y a="1">     <z a="1"/>   </y> </x> 

merge with

<x>   <y a="1">     <z a="2"/>   </y>   <y a="2"/> </x> 

results in:

<x>   <y a="1">     <z a="1"/>     <z a="2"/>   </y>   <y a="2"/> </x> 

Looking good so far... :)


Followup:

This topic is a little old now, so I wanted to point out that VisualStudio 2010 has a feature to do web.config transformations built-in: http://msdn.microsoft.com/en-us/vstudio/Video/ff801895

Of course in typical Microsoft fashion of only implementing any feature 50% of the way, it only works for web projects using web deploy. There is a plugin to enable transformations in other projects, located here: http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx

You could also use a tool like BuildMaster to manage config files (along with builds, tests, DB scripts, etc...)

like image 528
CodingWithSpike Avatar asked Jan 09 '09 15:01

CodingWithSpike


People also ask

How do you create a web config for different environments?

In order to create the web. config transformations, locate the web. config file in the ASP.NET project, and right-click the item in the solution explorer. Notice the menu item “Add Config Transforms”.

Can we have multiple web config file in one solution?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration. For example if you have a application which has two modules lets say accounts and sales.

How many web config files can you have in your project and access the specific config file?

You can have 1 Web. config file per folder . So in your root you can have a web. config and in a subfolder you can have another one that overrides settings from the one in the root.


1 Answers

We split out all region specific settings into thier own config file. Under the root of the web app we create a config folder and place the region specific settings there. So whatever files are living under the root of config will get picked up.

our web.config looks something like:

. . . <appSettings configSource="config\appSettings.config"/> <nlog configSource="config\nlog.config"/> <applicationSettings>     <MyApp.UI.Properties.Settings configSource="config\Settings.APGUI.config"/>     <MyApp.BusinessServices.Properties.Settings configSource="config\Settings.Business.config"/>     <MyApp.Auditing.Properties.Settings configSource="config\Settings.Auditing.config"/> </applicationSettings> . . . 

So if we are deploying to the release region the build tool will just have an action to replace the files in the root of config with the files from the appropriate region folder. The file structure looks something like:

ADDED: This is how the source control structure looks, the deployed app would just have the config dir with no sub folders or course

\Root    web.config        \Config            appsettings.config            services.config            logging.config            \release               appsettings.config               services.config               logging.config            \debug           appsettings.config               services.config               logging.config 

It is pretty clean and supported by any automated build tool(copying/replacing files). The nice side effect is that developers can create different flavors and keep them under source control without affecting the "real" configs.

like image 115
Dan Avatar answered Oct 09 '22 21:10

Dan