Creating transformations for custom config files
Visual Studio provides native support to add application config transforms. But it is restricted to the native configuration files only. Also by default it only provides transforms for debug and release environment.
If you need to use your own custom configuration file for custom defined environment, below are two options you can follow:
Let's assume, we have a configuration file: Configuration.xml in a project. We want to add Configuration.Staging.xml and Configuration.Production.xml as transforms.
To achieve this, I can add those xml files manually first and then edit the project file (.csproj) as below and then re-load the project:
<ItemGroup>
<Content Include="Configuration.xml">
<SubType>Designer</SubType>
<TransformOnBuild>true</TransformOnBuild>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Configuration.Staging.xml">
<DependentUpon>Configuration.xml</DependentUpon>
<IsTransformFile>True</IsTransformFile>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Configuration.Production.xml">
<DependentUpon>Configuration.xml</DependentUpon>
<IsTransformFile>True</IsTransformFile>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
After re-loading the project, you should've find the transformation files linked to Configuration.xml.
Now, it will work for the default configuration files that comes with visual studio project templates. So if you right click on the config file you will have a menu for adding transforms:
But to be able to add transforms for custom configuration files, you will need to add some extension/plugin. I have used SlowCheetah - XML Transforms. Using this plugin, you will be able to add transforms (from build configurations) to custom xml files as well:
Apart from adding the transforms, SlowCheetah will also allow you to visualize the transformations in the other environments:
After successfully adding the transformation files, do not forget to edit them with actual values! If you want to know more about the transformation syntax, you can have a look in here.
Hope it helps :)
If you need to use your own custom configuration file for custom defined environment, below are two options you can follow:
Option 1: Do it "manually"
Let's assume, we have a configuration file: Configuration.xml in a project. We want to add Configuration.Staging.xml and Configuration.Production.xml as transforms.
To achieve this, I can add those xml files manually first and then edit the project file (.csproj) as below and then re-load the project:
<SubType>Designer</SubType>
<TransformOnBuild>true</TransformOnBuild>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<DependentUpon>Configuration.xml</DependentUpon>
<IsTransformFile>True</IsTransformFile>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Configuration.Production.xml">
<DependentUpon>Configuration.xml</DependentUpon>
<IsTransformFile>True</IsTransformFile>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
After re-loading the project, you should've find the transformation files linked to Configuration.xml.
Option 2: Using Build Configuration
We can use Configuration Manager dialog from the Build menu to create new configurations for our own custom environments:
I've added two new configurations for Production and Staging
environment. Remember to check the "Create new project configurations"
while creating them:
Now, it will work for the default configuration files that comes with visual studio project templates. So if you right click on the config file you will have a menu for adding transforms:
But to be able to add transforms for custom configuration files, you will need to add some extension/plugin. I have used SlowCheetah - XML Transforms. Using this plugin, you will be able to add transforms (from build configurations) to custom xml files as well:
Apart from adding the transforms, SlowCheetah will also allow you to visualize the transformations in the other environments:
After successfully adding the transformation files, do not forget to edit them with actual values! If you want to know more about the transformation syntax, you can have a look in here.
Hope it helps :)
Comments
Post a Comment