Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 347
Wallace Breza Avatar asked May 11 '10 14:05

Wallace Breza


People also ask

How do I use different web config files?

config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release. If you want to transform other files you can do that too.

How do I create a new transformation in web config?

If you have a web application project, Right-click on web. config and choose Add Config Transform. This will add any config transforms that are missing from your project based on build configurations (i.e. if you have Production and Staging build configs, both will get a transform added).


1 Answers

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>
like image 199
Julien Hoarau Avatar answered Sep 19 '22 14:09

Julien Hoarau