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.