Managing Multiple Configuration File For Different Application Life cycles In .Net Applications

by Vahid 25. May 2009 16:50

All of us have already dealt with the problem of promoting configuration files in different application life cycles. this problem can be as simple as managing different connection strings for different databases in each environment to managing the other complicated settings in each environment.

so many times it happens that we forget to update the configuration file (web.config or app.config) before promotes. what we had already done was to keep one configuration file for each environment (development, testing, staging …) and after promote before build overwrite the the existing configuration file. but even in this case we used to miss to update the configuration file with the latest information. anyway i came across the very good solution in Scott Hanselman's blog. ok here is the solution.

  1. In your project Click the configuration management dropdown and select "Configuration Manager."image

You'll probably have Debug and Release configurations, but you can also make custom ones and base them on existing configuration. In this dialog I've made a new "Deploy" and I'll base it on the "Release" configuration.

WindowClipping (8)

Make sure to create a Solution Configuration AND a Project Configuration, as they are different. Here I've made one called Deploy for the Project also. If you get an error message, be aware of the "Create new project configurations" checkbox. You might get a warning if you are making a new configuration and the dialog tries to make another configuration with the same name; uncheck the checkbox if that happens. Of course, you can have as many Configurations as you'd like.

deploy

2. Add some custom configuration stuff in web.config, like connectionStrings:

	   1: <connectionStrings>
	
	   2:     <add name="Foo"
	
	   3:          connectionString="Data Source=localhost;Initial Catalog=DatabaseName;
	
	   4:                            User Id=sa;Password=debug;"
	
	   5:          providerName="System.Data.SqlClient" />
	
	   6: </connectionStrings>
	

See now I've made the password in my nonsense connectionString = "debug"? Now, create three new web.config's by CTRL-dragging the web.config on top of the project. Name them web.config.debug, web.config.deploy, and web.config.release. Make the password equal to "deploy" and "release" respectively.

WindowClipping (10)

3. Ok,  now we've got different configuration and different configuration files. Let's create a batch file called "copyifnewer.bat" and here's the contents:

	   1: @echo off
	
	   2: echo Comparing two files: %1 with %2
	
	   3:  
	
	   4: if not exist %1 goto File1NotFound
	
	   5: if not exist %2 goto File2NotFound
	
	   6:  
	
	   7: fc %1 %2 
	
	   8: if %ERRORLEVEL%==0 GOTO NoCopy
	
	   9:  
	
	  10: echo Files are not the same.  Copying %1 over %2
	
	  11: copy %1 %2 /y & goto END
	
	  12:  
	
	  13: :NoCopy
	
	  14: echo Files are the same.  Did nothing
	
	  15: goto END
	
	  16:  
	
	  17: :File1NotFound
	
	  18: echo %1 not found.
	
	  19: goto END
	
	  20:  
	
	  21: :File2NotFound
	
	  22: copy %1 %2 /y
	
	  23: goto END
	
	  24:  
	
	  25: :END
	
	  26: echo Done.
	

Basically this batch file will copy a file over another if the files don't match. It's not strictly "copyifnewer" (like, not at all) but it does the job.

Why bother with a batch file to check for changes and not just copy over the file every time? Well, each time you copy over a web.config it restarts all the designers and running AppDomains that are watching that file. No need to copy over a file if it hasn't changed...everything will churn less.

Put this copyifnewer.bat file in the root of your project.

WindowClipping (10)

4. Create a Pre-build Event. Right-click on your Project and select Properties. Click Build Events and in the "Pre-build event command line" and enter this value:

	"$(ProjectDir)copyifnewer.bat" "$(ProjectDir)web.config.$(ConfigurationName)" "$(ProjectDir)web.config"
	

Notice the magic dust, the $(ConfigurationName) project variable, that contains "Debug" or "Release" or "Deploy."

WindowClipping (9)

5. Build. Now if you build, you'll see in the Build Output the batch file being run and the files being copied. Because it's a Pre-Build Event it'll be seen in both the Build Output in Visual Studio .NET.

When you build within Visual Studio the currently selected item in the drop-down list is the current configuration.  now you have different configuration file for each build

there is catch here and that is we have to remember that we've got to keep web.config's in sync if there's lots of settings, but we could totally break it apart via "include files."

Tags: , ,

.Net | Technical

Comments

4/18/2011 6:27:06 PM #

replica pens

Promotional pens, custom printed pens and logo pens at cheap prices.

replica pens United States

4/18/2011 6:27:46 PM #

replica pens

Low cost Quality custom printed pens and pencils. Great prices on these quality writing instruments.

replica pens United States

4/18/2011 9:02:16 PM #

Sidney Arcino

I wanted to follow up and let you know how , a great deal I valued discovering your blog today. I would consider it an honor to work at my business office and be able to utilize the tips shared on your site and also engage in visitors' opinions like this. Should a position associated with guest article writer become on offer at your end, i highly recommend you let me know.

Sidney Arcino United States

4/19/2011 7:17:41 AM #

Odzyskiwanie Danych

It is a pity that I do not have such ideas :/

Odzyskiwanie Danych United States

4/19/2011 7:46:24 AM #

Chester Galassi

Thanks so much for writing all of the excellent information! Looking forward to checking out more posts!

Chester Galassi United States

4/19/2011 9:48:20 AM #

meble biurowe

First of all snowdrops -thin wisps of bright green against the grey earth – soon their

meble biurowe United States

4/19/2011 12:59:22 PM #

Reviews

I would like to point out my passion for your kindness in support of folks that need guidance on this important field. Your real commitment to passing the message all through became surprisingly helpful and have without exception helped ladies much like me to reach their pursuits. Your entire warm and helpful tips and hints means a lot to me and far more to my fellow workers. Warm regards; from each one of us.

Reviews United States

4/19/2011 6:34:57 PM #

iPhone 4 Review

I am also writing to make you be aware of of the fine experience my cousin's daughter encountered reading the blog. She mastered many issues, most notably what it is like to possess an ideal teaching style to let men and women without hassle have an understanding of specific impossible topics. You actually exceeded my expected results. Thanks for imparting such warm and friendly, healthy, edifying and as well as cool thoughts on your topic to Evelyn.

iPhone 4 Review United States

4/19/2011 10:59:34 PM #

Easy Click Commissions

Whats up, I'm ranking awesome for "Easy Click Commissions".

Easy Click Commissions United States

4/20/2011 3:09:10 AM #

blow kesha lyrics

Thanks so much for sharing all of the awesome info! I am looking forward to checking out more posts!

blow kesha lyrics United States

4/20/2011 4:51:08 AM #

blow kesha lyrics

Very useful info provided Thanks for it!

blow kesha lyrics United States

4/20/2011 5:11:42 AM #

blow kesha lyrics

Very useful info provided Thanks for it!

blow kesha lyrics United States

4/20/2011 5:32:06 AM #

blow kesha lyrics

Thanks so much for sharing all of the awesome info! I am looking forward to checking out more posts!

blow kesha lyrics United States

4/20/2011 8:39:32 AM #

John Ratsep

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

John Ratsep United States

4/20/2011 10:55:53 AM #

patricia

J'ai vraiment aimé votre sujet, merci d'avoir écrit cet article. Je reviendrai souvent sur votre blog pour lire vos nouveaux articles.

patricia United States

4/20/2011 12:41:23 PM #

veronik

J'ai aimé lire votre article, c'est gentil de nous avoir montré ce sujet. Je passerai souvent sur votre site pour lire vos nouveaux articles.

veronik United States

4/20/2011 3:27:38 PM #

tarot divinatoire gratuit

J'ai vraiment aimé votre texte, merci d'avoir écrit cet article. Je passerai souvent sur votre blog pour lire vos nouveaux sujets.

tarot divinatoire gratuit United States

4/20/2011 7:21:10 PM #

tarot divinatoire gratuit

J'ai aimé lire votre texte, c'est gentil de l'avoir partagé avec nous. Je reviendrai souvent sur votre site pour voir vos nouveaux sujets.

tarot divinatoire gratuit United States

4/20/2011 8:38:48 PM #

Marion Moon

Wow Keep it upI have bookmarked your site for future use and would recommend everyone do the same.

Marion Moon United States

4/23/2011 8:31:31 PM #

search engine optimization delhi

I got what you  intend, saved to bookmarks , very nice   website  .

search engine optimization delhi United States

4/24/2011 11:01:12 PM #

Issac Maez

I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

Issac Maez United States