XDT instances for non-web projects and xdt instances for web projects
Overview
XDT is a feature of Asp.net 4.0. It allows users to quickly switch configuration files under Different generation types in Web projects (for example, using the test configuration database in debug scenarios, in the Release scenario, use the formal configuration database ). However, in non-web projects, VS does not provide such convenient functions. If we want to use the xdt transforms function, we need to configure the MSbuild file.
In this example, we modify the MSBuild configuration file so that non-web projects can also use the XDT function.
Microsoft Build Engine is a platform for building applications. It can be simply understood as the configuration of the project generated by Vs, And the configuration information can be used to perform specific operations on the project file.
Project Preparation
Create a console project and create the following files in the project:
The content of app. config
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=TestSQLServer;Initial Catalog=MyTestDB;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>
App. debug. config content
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=Debug;Initial Catalog=MydebugDB;Integrated Security=True" providerName="System.Data.debug" xdt:Transform="Replace" xdt:Locator="Match(name)"/></connectionStrings>
App. release. config content
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=Debug;Initial Catalog=MydebugDB;Integrated Security=True" providerName="System.Data.debug" xdt:Transform="Replace" xdt:Locator="Match(name)"/></connectionStrings>
Output the configuration file you want to see in the console Program
private static readonly string DifferentConfigString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; static void Main(string[] args) { Console.Write(DifferentConfigString); Console.Read(); }
The final presentation of the entire project structure is as follows (config will be changed to the following after specific configuration ):
Specific implementation:
1. Reference Msbuild in the project
<Import Project = "$ (MSBuildExtensionsPath) \ Custom \ TransformFiles.tar gets"/>
5. In the project file, set TransformOnBuild to true for the files you want to convert to metadata.
In this example, modify <None Include = "App. config"/>
<None Include="App.config"> <TransformOnBuild>true</TransformOnBuild></None>
Modify <None Include = "App. Debug. config"/> <None Include = "App. Release. config"/>
<None Include="App.Debug.config"> <DependentUpon>app.config</DependentUpon> </None> <None Include="App.Release.config"> <DependentUpon>app.config</DependentUpon></None>
6. Reload the project
7. Switch the solution configuration and run the program to find that the configuration file will automatically use the content in different app. config.
Example
Use debug parameters automatically in the debug environment
The release parameter is automatically used in the release version.
Implementation Principle
TransformFile.tar gets contains two targets: DiscoverFilesToTransform and TransformAllFiles.
. DiscoverFilesToTransform browses all items (None, Content, and Resource ). In DiscoverFilesToTransform, I find the value that contains % (TransformOnBuild) = true. After all the values are collected, identify whether there is an "app. config will be converted. If yes, put it in a special item list, and put other items in another item list.
In TransformAllFiles, The TransformXml task is used to convert all files. Use the AfterTargets attribute = "Build; _ CopyAppConfigFile" to inject the target into the generation process. The TransformAllFiles target is executed every time the object is generated or _ CopyAppConfigFile is called.
The following is all the code of the transformfiles.tar gets file.
<?xml version="1.0" encoding="utf-8"?><Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.Tasks.dll"/> <ItemDefinitionGroup> <!-- Set the default value to false here --> <None> <TransformOnBuild>false</TransformOnBuild> </None> <Content> <TransformOnBuild>false</TransformOnBuild> </Content> <Resource> <TransformOnBuild>false</TransformOnBuild> </Resource> <EmbeddedResource> <TransformOnBuild>false</TransformOnBuild> </EmbeddedResource> <_FilesToTransform> <IsAppConfig>false</IsAppConfig> </_FilesToTransform> </ItemDefinitionGroup> <PropertyGroup> <TransformAllFilesDependsOn> DiscoverFilesToTransform; </TransformAllFilesDependsOn> </PropertyGroup> <Target Name="TransformAllFiles" DependsOnTargets="$(TransformAllFilesDependsOn)" AfterTargets="Build;_CopyAppConfigFile"> <!-- Now we have the item list _FilesToTransformNotAppConfig and _AppConfigToTransform item lists --> <!-- Transform the app.config file --> <ItemGroup> <_AppConfigTarget Include="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /> </ItemGroup> <PropertyGroup> <_AppConfigDest>@(_AppConfigTarget->'%(FullPath)')</_AppConfigDest> </PropertyGroup> <MakeDir Directories="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)')" Condition="Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')"/> <TransformXml Source="@(_AppConfigToTransform->'%(FullPath)')" Transform="%(RelativeDir)%(Filename).$(Configuration)%(Extension)" Destination="$(_AppConfigDest)" Condition=" Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)') " /> <TransformXml Source="@(_FilesToTransformNotAppConfig->'%(FullPath)')" Transform="%(RelativeDir)%(Filename).$(Configuration)%(Extension)" Destination="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')" Condition=" Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)') " /> </Target> <Target Name="DiscoverFilesToTransform"> <!-- This will look through items list: None & Content for those with Metadata <TransformOnBuild>True</TransformOnBuild> --> <ItemGroup> <_FilesToTransform Include="@(None);@(Content);@(Resource);@(EmbeddedResource)" Condition=" '%(TransformOnBuild)' == 'true' "/> </ItemGroup> <PropertyGroup> <_AppConfigFullPath>@(AppConfigWithTargetPath->'%(RootDir)%(Directory)%(Filename)%(Extension)')</_AppConfigFullPath> </PropertyGroup> <!-- Now look to see if any of these are the app.config file --> <ItemGroup> <_FilesToTransform Condition=" '%(FullPath)'=='$(_AppConfigFullPath)' "> <IsAppConfig>true</IsAppConfig> </_FilesToTransform> </ItemGroup> <ItemGroup> <_FilesToTransformNotAppConfig Include="@(_FilesToTransform)" Condition=" '%(IsAppConfig)'!='true'"/> <_AppConfigToTransform Include="@(_FilesToTransform)" Condition=" '%(IsAppConfig)'=='true'"/> </ItemGroup> </Target></Project>
You can also download it here
Others
Understanding XDT: http://www.cnblogs.com/JustRun1983/p/3418844.html
Understanding MSbuild: http://www.cnblogs.com/l_nh/archive/2012/08/30/2662648.html
This article comes from: http://sedodream.com/2010/11/18/XDTWebconfigTransformsInNonwebProjects.aspx