The project I am working on involves reading a lot of service endpoints (url) from a config file. Since the list would be quite large I decided to keep them in a custom config file to keep my web.config clean and small. I included the custom section to my web as below:
<mySection configSource="myConfig.config" />
I works perfectly fine.
But the problem of transformation appears during the deployment of the project to different environments. I have three web.config files:
Web.config
Web.Uat.config
Web.Release.config
While the transformation web.config works, the transformations for custom config files fails at deployment.
Is there an way I can transform the custom config file during deployment?
I have been using SlowCheetah but I found something that I think is more elegant. Just telling to the build to generate the .config depending on the build configuration.
Having a app.Release.config in your project (or many more depending on you deployment needs) you just need to edit the project file (the .csproj one if you program in C#). Find the end of it, between the last
</ItemGroup>
and</Project>
and add:Save and reload from VisualStudio. Compile in Release mode and check the bin/Release folder on your
<MyProject>.config
file the transformation is done.This example applies to Exe and Dll files and any VisualStudio version because includes this post help
Visual Studio transforms only web.config files by default.
If you need custom config file with transformation for DEV, UAT, PROD, etc environments, then try to
A little bit details:
Add VS Extension SlowCheetah from Extensions and Updates
Right click on your myconfig.config and choose add transorm:
Inside each defined configurations insert your own transormation rulles like that:
Hope it was helpful
There is another approach that doesn't require installing extensions nor using build events.
Let's suppose you have your custom configs like so:
Then in your main
Web.config
you have this:Lastly, inside your
Web.Uat.config
you add a transform like this:This is not transforming the
myConfig.config
file, but rather overriding the name of the custom config file that should be used. You can do the same for the Release and any other environments.Your
myConfig.Uat.config
should not contain transformations, it should be a copy of the base custom config file, with the appropriate values for the custom environment.The downside is everytime you add something to the base custom config file, you need to also add to the config files for other envs (even if the value should be the same through envs). So I'd consider just using these custom config files for settings that should be changed between envs.
I'm going to extend on Andoni Ripoll Jarauta's answer a little.
We were faced with a similar problem. I wanted to pull the connection strings out of the web.config file to limit merge conflicts. I also wanted create a "release" config containing static information when publishing.
...simple enough. Create a custom config file, webdb.config, and update the web.config file.
Ex. web.config
wedbdb.config (xml version="1.0" is required for transformation)
Next add transformation files for webdb.config
WebDB.Debug.config example:
WebDB.Release.config example:
Next we need to add an after-build event. This is created by simply editing the CSPROJ file.
Now when I run locally I'll get WebDB.Debug.config and when I publish my code I just need to make sure to select "Release" as the configuration source. In both cases the WebDB.config file will be updated with the corresponding file when you build.
NOTE: make sure you set the webdb.config, webdb.debug.config, and webdb.release.config to "Do not copy" for the "Copy to Output Directory" option.
Hope this helps!