Reputation: 2313
I have a scenario where the database/connection string needs to be changed in the web.config file of my application depending on where the application is currently residing. That is, my Dev web server should be connected to my Dev database. The same applies for my Test and Production environment. Through some research, it seems that web.config transformations is exactly what I am looking for, but there is one potential issue.
The company I work for uses a homebrew method for deploying production applications. What it does is copy files (user selected or calculated difference) from the Test environment to the Prod environment. Will using web.config transformations still work?
If I were to deploy to Test, then use our tool to "deploy" to Prod, the Test version of the web.config file would be copied over right? This is assuming the transformation is applied during build time.
I suppose it would be possible to deploy the Prod version to Test, then replace it with the proper version after Prod deployment, but that seems messy. Am I mistaken in the way transformations work? What would be a clever solution to this strange problem?
Upvotes: 1
Views: 2655
Reputation: 10120
@Jeff - I have been dealing with the same situation, our problem was that we didnt want to recompile the application everytime we migrate from one environment to another. That is one problem with web transformations (recompiling the code!).
We packaged the base web.config as well as the transformation files within the MSI. So we packaged web.config, web.config.DEV, web.config.Prod etc. Then after the MSI is installed, I updated our age old deployment script to call an MSBUILD script which does the transformation on the PROD server.
My powershell script would first find all the config files that end with the current environment name (.DEV, .PROD etc). Then it will find the default file, if it is web.config.dev it will try to find web.config. After this it will pass on these arguments to the MSBUILD and do the transformation. After the transformation the output will be saved in a default location which will be later copied back to the original location.
<UsingTask TaskName="TransformXml"
AssemblyFile="Microsoft.Web.Publishing.Tasks.dll"/>
<PropertyGroup>
<FilePath></FilePath>
<ConfigFileName></ConfigFileName>
<TransformFileName></TransformFileName>
<OutputFileName></OutputFileName>
<StackTraceEnabled>False</StackTraceEnabled>
</PropertyGroup>
<Target Name="Transform">
<Message Text="FilePath = $(FilePath)" />
<Message Text="ConfigFileName = $(ConfigFileName)" />
<Message Text="TransformFileName = $(TransformFileName)" />
<Message Text="OutputFileName = $(OutputFileName)" />
<TransformXml Source="$(FilePath)$(ConfigFileName)"
Transform="$(FilePath)$(TransformFileName)"
Destination="$(OutputFileName)"
StackTrace="$(StackTraceEnabled)" />
</Target>
Upvotes: 0
Reputation: 1039508
Will using web.config transformations still work?
No, the transformation is executed when you use the Publish functionality in Visual Studio or MSBuild. Once you deploy a precompiled version of your application on a given environment (assuming you used the publish functionality) the other transformations are lost and not deployed to this environment. So if you use some home made tool to copy from TEST to LIVE you will not be able to take advantage of those transformations. For this to work you will need to re-publish your application using the LIVE environment.
Upvotes: 3