Tag: transformation
VStudio Web.config Transformation for your App.configs
by B.Russell on Jul.01, 2011, under Development, Tips and Tricks
I searched high and low for this tid-bit before finally stumbling upon this great question\answer over at Stack Overflow. I’m going to lay out the steps here for reference but credit goes to Oleg Sych and Dan Abramov.
Original article:
App.Config Transformation for projects which are not Web Projects in Visual Studio 2010?
1.) Add an XML file for each configuration to the project.
Let’s say your project build settings are Debug.Local, Debug.Production, Release.Local, Release.Production. Then your new app.config files should be titled as app.<build title>.config …. or app.Debug.Local.config, app.Debug.Production.config, etc. One file for each build configuration. Once they are added to your project continue to step 2.
2.) Unload project and open *.csproj file for editing
Right-click on the project and select Edit project file from the Solution Explorer.
3.) Bind app.configs created in Step 1 to original app.config
Find the section inside the project file that contains app.config and all the app.<build title>.config files you just manually added. Notice how their build actions are set to None.
- Set all the app config files build action to Content
- Make all the app config files you manually added dependent upon the original app.config file.
When you finished it should look something like this:
<Content Include="App.config" />
<Content Include="App.Debug.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
<Content Include="App.Release.config" >
<DependentUpon>App.config</DependentUpon>
</Content>
4.) Activate transformation magic
In the end of the file after
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
and before final
</Project>
Insert the following XML:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
<!-- Generate transformed app config in the intermediate directory -->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!-- Force build process to use the transformed configuration file from now on. -->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
Now you can reload the project. Make sure that all the manual app.<build settings>.config files you added earlier are now neatly tucked away under the original app.config file.
Build and enjoy!