How to add config transformations for a custom config file in Visual Studio?

TejSoft picture TejSoft · Jan 12, 2016 · Viewed 29.6k times · Source

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?

Answer

Michael picture Michael · Jan 12, 2016

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

  1. Use custom extensions for Visual Studio like SlowCheetah - XML Transforms for Config transformation preview functionality.
  2. Add for the project from Nuget SlowCheetah to provide build in transformation.

A little bit details:

Add VS Extension SlowCheetah from Extensions and Updates Screen of Extensions and Updates

Right click on your myconfig.config and choose add transorm: Screen of Extensions and Updates

Inside each defined configurations insert your own transormation rulles like that:

<services xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <service name="WebApplication1.Services.Service2" xdt:Transform="Replace" xdt:Locator="Match(name)" >
    <endpoint address="http://localhost:57939/Services/DebugService" behaviorConfiguration="WebApplication1.Services.Service2AspNetAjaxBehavior"
      binding="webHttpBinding" contract="WebApplication1.Services.Service2" />
  </service>
</services>

Hope it was helpful