Configuration Transformation gives the ability to run our project with multiple configuration for different build environment which have different connection string i.e. databases credentials ..... Multiple configuration files for different environments (e.g. dev/staging/production).
We can use different tools to create configuration files.
1. Slow Cheetah.
2. Configuration Transform Extension
Step to Create Configuration Files.
. Bind App.*.config files to main App.config
Find the project file section that contains all
Next, make all configuration-specific files dependant on the main
Replace XML above with the one below:
In the end of file after
Now you can reload the project, build it and enjoy
We can use different tools to create configuration files.
1. Slow Cheetah.
2. Configuration Transform Extension
Step to Create Configuration Files.
1. Add App.config file to project
2. Right click App.config file in solution explorer and select Add Config Transforms
Notice that you have new App.$$$.config files under App.config
corresponding to build configurations
3. Set the source App.config
XML
<?xml version="1.0" encoding=" utf-8" ?>
<configuration>
<appSettings>
<add key="Environment" value=""/>
</appSettings>
</configuration>
4. Set the target App.Debug.config
XML
<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http:// go.microsoft.com/fwlink/? LinkId=125889 -->
<configuration xmlns:xdt="http ://schemas.microsoft.com/XML- Document-Transform">
<appSettings>
<add key="Environment" value="Debug" xdt:Transform=" Replace" xdt:Locator="Match( key)"/>
</appSettings>
</configuration>
5. Build project.
Linked Configs Step by Step Instructions:
1. Add link(Existing Item...) App.config file to project
2. Right click link App.config file in solution explorer and select Add Config Transforms
Click Yes button, if you select No button concrete(not linked) config files will be created which suits certain use case
Notice that you have new App.$$$.config files under App.config
corresponding to source App.config files
3. Continue with step 3 above...
Trouble Shooting: After doing this code will not debug . For This we are supposed to unload .csproj file and need to remove is Transform File tag. And below (4th one heading code in .cs
Find the project file section that contains all
App.config and App.*.config references. You'll notice their build actions are set to None:<None Include="App.config" />
<None Include="App.Debug.config" />
<None Include="App.Release.config" />
First, set build action for all of them to Content.Next, make all configuration-specific files dependant on the main
App.config so Visual Studio groups them like it does designer and codebehind files.Replace XML above with the one below:
<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 transformations magicIn the end of 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, build it and enjoy
App.config transformations!
No comments:
Post a Comment