Create Multiple app.config files and run them with pre build events & msbuild

 

Today we were stumped with having different config files for automation for different environments like QA, Daily runs, BDT etc . While there are various ways, we could have resolved this, each of the methods we thought had one or the other challenge as we needed a single simple solution so that we could service 20 products line.

Also, making changes to the code base did not make sense. So here is how we achieved it

1.  Go to the solution – > right click choose configuration manager. In that screen use the drop down for active solution configuration and choose new. You will be prompted with the new solution configuration as shown below. We added DailyRun as our name.

image

2. Now go ahead and create any number of configuration files you want. Here in our case, we have BDT, DailyRun and Release1 as the configurations for different environment.

(side note : in our solution we had set the original app.config as a content and property always copy. The other configs were left out as it is. As app.config is the main file and that is the one which does all the hard work)

image

3.  Let's create a batch file called "copyalways.bat" and here's the contents:

Put this copyalways.bat file in the root of your project. Basically this batch file will copy a file over another if the files don't match.

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:




5. 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.

 

And there you go. The connection string in the web.config now contains deployment-specific configuration data.

You can add only the parameter part to your build definition now. This will help you run the same solution with different config files.



(Note: we noticed that once the newer app.config is copied , the file was becoming read-only and was not taking in the next build config. The way to solve it would be to make it non read only within the code itself. I will post that code soon).

Done. Here is the complete code of the bat file

www.CodeNirvana.in

Powered by Blogger.

Translate

Total Pageviews

Copyright © T R I A G E D T E S T E R