Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web.config transform is running twice on publish

I have a solution that includes three web projects (as well as a lot of class library projects). The web projects all use Web.config transforms to specify per-environment configuration.

I have Web.config transforms for multiple build profiles, named Web.UAT.config, Web.Staging.config and Web.Release.config

I am building and deploying the project from my CI server using MSBuild with the following arguments:

/t:Clean,Build /p:Configuration=UAT;DeployOnBuild=True;PublishProfile=UAT

For exactly one of the three projects, the web.config transforms appear to get applied twice, with elements marked xdt:Transform="Insert" appearing twice. Looking in the build output, it seems that all three projects run the following targets:

PreTransformWebConfig
TransformWebConfigCore
PostTransformWebConfig
PreProfileTransformWebConfig

But the problematic project also runs these targets (immediately after those listed above):

ProfileTransformWebConfigCore
PostProfileTransformWebConfig
like image 376
hgcummings Avatar asked Jul 21 '15 15:07

hgcummings


1 Answers

The .csproj files for web projects include the following by default:

<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />

This file in turn imports \Web\Microsoft.Web.Publishing.targets, also under the VSToolsPath (on my dev machine, this corresponds to C:\Program Files (x86)\MSBuild\VisualStudio\v12.0).

The interesting segment of this file looks like the following:

<ProjectProfileTransformFileName Condition="'$(ProjectProfileTransformFileName)'=='' And '$(PublishProfileName)' != '' ">$(_ProjectConfigFilePrefix).$(PublishProfileName)$(_ProjectConfigFileExtension)</ProjectProfileTransformFileName>

<!--if $(TransformWebConfigEnabled) is also enabled and the ConfigTransform and ProfileTransform happen to have same filename, we default $(ProfilefileTransformWebCofnigEnabled) to false so it doesn't do double transform-->
<ProfileTransformWebConfigEnabled Condition="'$(ProfileTransformWebConfigEnabled)'=='' And '$(TransformWebConfigEnabled)' == 'true' And ('$(ProjectProfileTransformFileName)' == '$(ProjectConfigTransformFileName)')">False</ProfileTransformWebConfigEnabled>

The double transform was happening as a result of ProfileTransformWebConfigCore running, which is conditional on ProfileTransformWebConfigEnabled, which only defaults to false if the ProjectProfileTransformFileName and ProjectConfigTransformFileName are equal.

I added the following target to all three of my projects:

  <Target Name="DebugWebConfigTransform" AfterTargets="PreProfileTransformWebConfig">
    <Message Text="ProjectProfileTransformFileName: $(ProjectProfileTransformFileName)"/>
    <Message Text="ProjectConfigTransformFileName: $(ProjectConfigTransformFileName)"/>
  </Target>

For the problematic project, this target output the following:

DebugWebConfigTransform:
  ProjectProfileTransformFileName: Web.UAT.config
  ProjectConfigTransformFileName: Web.Release.config

Since these two values were different, the double transform was occuring for the reasons described above.

The reason the ProjectConfigTransformFilename was set to Web.Release.config was that the ProjectConfigurationPlatforms in my .sln file was incorrect. The .sln file's Configuration|Platform pair of UAT|Any CPU was being mapped to Release|Any CPU for this project.

I think it was actually applying the UAT and Release transforms as a result (due to the exact nature of my transforms and the order in which they were applied, this was indistinguishable from applying the UAT transform twice).

Updating the ProjectConfigurationPlatforms mapping in the solution file resolved the issue for me.

like image 200
hgcummings Avatar answered Nov 03 '22 01:11

hgcummings