Saltear al contenido principal
Configuration Transforms In Visual Studio

Configuration transforms in Visual Studio

  • Blog

One of the most useful tools on Visual Studio web-based projects is the ability to create configuration transforms, generating files with a specific build or environment in mind. However, out-of-the-box, there is no way to use this feature in applications, services or libraries.

Well, not directly… But if you tinker around with the project file within your solution, you can add this feature to your builds! Let’s take a step by step look to create a single transformation file for the “Release” configuration.

Start by including the transformation file in your project, just as you would add another XML file.

Configuration transforms in Visual Studio

Now, right-click your project, and unload the application. This will allow you to edit directly the definition file.

Configuration transforms in Visual Studio

Within the file, search for the following entry:

<ItemGroup>
<None Include=”App.config” />
<None Include=”App.Release.config” />
</ItemGroup>

And change it into:

  <ItemGroup>
<None Include=”App.config” />
<None Include=”App.Release.config”>
<DependentUpon>App.Config</DependentUpon>
</None>
</ItemGroup>

This will “nest” the specific configuration file within the general configuration file. If you need additional files, include them now, and update the XML accordingly.

Now we need to specify which of our *.Config files is the main project’s configuration file. To do so, go back to the beginning of the file, and make sure to include <ProjectConfigFileName>App.Config</ProjectConfigFileName> inside the first <PropertyGroup> tag.

The next step is to locate the first entry that begins with <Import Project=”, and then include the following entries:

<Import Project=”$(VSToolsPath)\Web\Microsoft.Web.Publishing.targets” Condition=”‘$(VSToolsPath)’ != ”” />
<Import Project=”$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v{XX}.0\Web\Microsoft.Web.Publishing.targets” Condition=”false” />

Make sure to replace the {XX} token with the Visual Studio version that will execute this job. For example, if you are using Visual Studio 2015, the second import sentence would be:

<Import Project=”$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets” Condition=”false” />

In case you are unsure about the assembly version of your Visual Studio installation, just take a look at the “About…” option inside the Help menu of the application, as shown in the following figure.

Configuration transforms in Visual Studio

Now, move to the end of the file, to the closing </Project> tag, and include the following custom task to use the two import target we just included on the previous step:

<UsingTask TaskName=”TransformXml” AssemblyFile=”$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v{XX}.0\Web\Microsoft.Web.Publishing.Tasks.dll” />
<Target Name=”AfterCompile” Condition=”exists(‘app.$(Configuration).config’)”>
<TransformXml Source=”app.config” Destination=”$(IntermediateOutputPath)$(TargetFileName).config” Transform=”app.$(Configuration).config” />
<ItemGroup>
<AppConfigWithTargetPath Remove=”app.config” />
<AppConfigWithTargetPath Include=”$(IntermediateOutputPath)$(TargetFileName).config”>
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
<AppConfigWithTargetPath Include=”$(IntermediateOutputPath)$(TargetFileName).config”>
<TargetPath>$(TargetName).vshost$(TargetExt).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>

Again, make sure to replace the {XX} tokens with the correct version of your Visual Studio.

That’s it! Now you can save the file, close the tab, and right-click on the project to reload it. You’ll know that the modifications worked if you see the App.Release.config file nested inside the general App.config file.

Configuration transforms in Visual Studio

Now, our App.config looks like this:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.5.2″ />
</startup>
<appSettings />
</configuration>

Let’s change it so that we have an additional key on the “appSettings” block. We edit our App.Release.Config file so that it contains:

<?xml version=”1.0″?>
<configuration xmlns:xdt=”http://schemas.microsoft.com/XML-Document-Transform”>
<appSettings>
<add key=”Transformed” value=”K-Lagan” xdt:Transform=”Insert”/>
</appSettings>
</configuration>

If we compile (making sure that “Release” is our selected configuration), the generated file will look like the following:

<?xml version=”1.0″ encoding=”utf-8″?>
<configuration>
<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.5.2″ />
</startup>
<appSettings >
<add key=”Transformed” value=”K-Lagan”/>
</appSettings>
</configuration>

Now we have another tool that allows us to automatize settings such as IPs, datasets and specific machine values, directly from your IDE. A bit of manual work goes a long way to reduce human error on repetitive tasks.

 

Written by Álvaro G. Cachón

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Volver arriba