0

I have a windows application developed in c# which has 3 config files namely dev.config, qa.config and prod.config.

In my main app.config, I have written something like this:

<appSettings configSource="dev.config" />

And when ever the I have deploy in different environment , I edit the app.config file from the binaries, change it to appropriate config file and deploy it (copy paste the binaries in the VM).

To automate this, at first , I have added a pre-build event which replaces the complete app.config file to appropriate environment file using xcopy, as mentioned in this answer.
The problem with this is , I had to build the code 3 times for deploying in 3 different environments.

MSBuild.exe "C:\test\TestProject.sln" /t:Rebuild /p:Configuration=dev
MSBuild.exe "C:\test\TestProject.sln" /t:Rebuild /p:Configuration=qa
MSBuild.exe "C:\test\TestProject.sln" /t:Rebuild /p:Configuration=prod

This creates 3 binaries separately in 3 different folder dev, qa, prod in bin folder.

I have also read about slow cheetah, which also does something similar.I have to build the code again and again for different environment based on the Configuration Manager.

I don't know if this is possible, but my requirement is to only build once and deploy the same binaries in different environments.In the server wherever I'm deploying, I will have a system environment variable set like:

environmentType = dev or qa or prod 

And somewhere in the code I should read the enviroment variable

System.Environment.GetEnvironmentVariable("environmentType")

and map to the appropriate config file. In short , there should not be any manual intervention for changing anything , or should not be building multiple times.

Or is there any way I can read the environment variable in app.config and write something like :

if env = "dev"
 <appSettings configSource="dev.config" />
else if env ="qa"
 <appSettings configSource="qa.config" />
else 
 <appSettings configSource="prod.config" />

Any guidance or help in this regard is highly appreciated.

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
  • A common way to handle this is to create all the config files during the build and rename the file for the required environment during deployment. – iakobski Nov 19 '18 at 06:24
  • You could pass the config file(s) as arguments (or default to prod, but swap it out if one is specified as an argument) – ProgrammingLlama Nov 19 '18 at 06:46
  • @John where should I pass the argument? – CrazyCoder Nov 19 '18 at 06:49
  • [Question on how to pass command line arguments](https://stackoverflow.com/questions/1179532/how-do-i-pass-command-line-arguments-to-a-winforms-application) – ProgrammingLlama Nov 19 '18 at 06:52
  • @John ok I understood what you meant by arguments. Say I have passed the argument. Where and how will I write code to swap the config file? Can you please explain in detail. – CrazyCoder Nov 19 '18 at 07:01
  • 1
    There are some [hacks](https://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime) for it, which is why I suggest defaulting to prod and only swapping for the other environments. – ProgrammingLlama Nov 19 '18 at 07:05
  • 2
    I'm not sure what application you are developing but I see your question is tagged as windows-services which makes me believe there is no UI. If that's the case I would seriously look at having your code ported to a C# .net core project. In C# .net core you can run everything using ASPNCETCORE_ENVIRONMENT=qa/dev/prod and it will run with different json files named appsettings.[qa/dev/prod].json I converted to .net core a year ago and the transition is not big and almost everything is for the better. https://andrewlock.net/how-to-set-the-hosting-environment-in-asp-net-core/ – Martin Florin Nov 19 '18 at 07:24

0 Answers0