简体   繁体   中英

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

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.

This issue was occurring for me because I had multiple projects in my solution configuration using different configurations.

It was running more than one web.config transforms because of this configuration:

配置管理器

After switching the projects to use the same configuration I no longer received the issues in my web.config.

It seems this can also happen if you leave off the Configuration msbuild parameter. The PublishProfile isn't good enough.

I had a different issue than all the answers. In my case, I had a profile named staging.pubxml which was using the configuration prod . On publish both the staging and the prod transformation would occur.

Turns out, the name of the .pubxml file also triggers a transform if the same configuration can be found. I simply changed the name of the file and it solved my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM