How to use the new VS 2010 configuration transform

2019-02-02 19:09发布

问题:

I have setup some configuration transforms in my web.config for my connectionStrings, etc. But I have separated out some areas of my web.config into separate files, ex) appSettings.config.

How can I configure Visual Studio and MSBuild to perform config transformations on these additional config files?

I have already followed the approach of the web.config to relate the files together within my web application project file, but transformations are not automatically applied.

<ItemGroup>
    <Content Include="appSettings.Debug.config">
        <DependentUpon>appSettings.config</DependentUpon>
    </Content>
</ItemGroup>

回答1:

By default the target managing the transformation (TransformWebConfig) works only on web.config file.


To make it work on your appSettings.config file you'll have to :

  • Set the Build Action of your file to Content
  • Call the MSBuild target TransformWebConfig with ProjectConfigFileName=appSettings.config and Configuration=$(Configuration).

To call MSBuild TransformWebConfig target for appSettings.config just after the transformation of web.config files, you need to add this at the end of your project file :

<PropertyGroup>
  <!-- Name of your custom config file -->
  <ConfigFileName>appSettings.config</ConfigFileName>
</PropertyGroup>

<PropertyGroup>
  <!-- 
      This property is used to handle circular dependency between
      TransformWebConfig and our custom target TransformAppConfig
  -->
  <FirstRun Condition="$(FirstRun) == ''">true</FirstRun>
</PropertyGroup>

<!-- This target will be called one time after a call to TransformWebConfig -->
<Target Name="TransformAppConfig" 
        AfterTargets="TransformWebConfig"
        Condition="$(FirstRun) == 'true'">

  <MSBuild Projects="$(MSBuildProjectFile)"
           Targets="TransformWebConfig"
           Properties="ProjectConfigFileName=$(ConfigFileName);
                       Configuration=$(Configuration);
                       FirstRun=false"/>
</Target>

<!-- 
    This target will be called one time before PreAutoParameterizationWebConfigConnectionStrings 
    to add $(ConfigFileName) to autoparameterization step
-->
<Target Name="AddToAutoParameterizationStep" 
        BeforeTargets="PreAutoParameterizationWebConfigConnectionStrings">
  <ItemGroup>
    <_WebConfigsToAutoParmeterizeCS Include="@(FilesForPackagingFromProject)"
                           Condition="('%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)') And !%(FilesForPackagingFromProject.Exclude)">
      <TransformOriginalFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
      <TransformOutputFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
      <TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
    </_WebConfigsToAutoParmeterizeCS>
    <_WebConfigsToAutoParmeterizeCSOuputFiles Include="@(_WebConfigsToAutoParmeterizeCS->'%(TransformOutputFile)')">
    </_WebConfigsToAutoParmeterizeCSOuputFiles>
  </ItemGroup>   
</Target>


回答2:

Something that makes this a lot easier, take a look at the SlowCheetah VS add-in at ... visualstudiogallery



回答3:

Here is the code that works for me:

    <PropertyGroup>
    <!-- Name of your custom config file -->
    <ConfigFileName>ConnectionStrings.config</ConfigFileName>
    <ConfigTransformFileName>ConnectionStrings.$(Configuration).config</ConfigTransformFileName>
  </PropertyGroup>
  <PropertyGroup>
    <!-- 
      This property is used to handle circular dependency between
      TransformWebConfig and our custom target TransformAppConfig
  -->
    <FirstRun Condition="$(FirstRun) == ''">true</FirstRun>
  </PropertyGroup>
  <Target Name="AddConfigToTransform" AfterTargets="CollectWebConfigsToTransform">
    <ItemGroup>
      <WebConfigsToTransform Include="@(FilesForPackagingFromProject)" Condition="'%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)'">
        <TransformFile>%(RelativeDir)$(ConfigTransformFileName)</TransformFile>
        <TransformOriginalFile>$(TransformWebConfigIntermediateLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
        <TransformOutputFile>$(TransformWebConfigIntermediateLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
        <TransformScope>$([System.IO.Path]::GetFullPath($(_PackageTempDir)\%(DestinationRelativePath)))</TransformScope>
      </WebConfigsToTransform>
    </ItemGroup>
  </Target>
  <!-- 
    This target will be called one time before PreAutoParameterizationWebConfigConnectionStrings 
    to add $(ConfigFileName) to autoparameterization step
-->
  <Target Name="AddToAutoParameterizationStep" BeforeTargets="PreAutoParameterizationWebConfigConnectionStrings">
    <ItemGroup>
      <_WebConfigsToAutoParmeterizeCS Include="@(FilesForPackagingFromProject)" Condition="('%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)') And !%(FilesForPackagingFromProject.Exclude)">
        <TransformOriginalFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
        <TransformOutputFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
        <TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
      </_WebConfigsToAutoParmeterizeCS>
      <_WebConfigsToAutoParmeterizeCSOuputFiles Include="@(_WebConfigsToAutoParmeterizeCS->'%(TransformOutputFile)')">
      </_WebConfigsToAutoParmeterizeCSOuputFiles>
    </ItemGroup>
  </Target>