Search code examples
visual-studio-2010msbuildweb-configslowcheetah

How to use the new VS 2010 configuration transforms and apply them to other .config files?


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>

Solution

  • 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>