Web.config 轉換在發佈時執行兩次
我有一個解決方案,其中包括三個 Web 項目(以及許多類庫項目)。Web 項目都使用 Web.config 轉換來指定每個環境的配置。
我有多個建構配置文件的 Web.config 轉換,名為 Web.UAT.config、Web.Staging.config 和 Web.Release.config
我正在使用帶有以下參數的 MSBuild 從我的 CI 伺服器建構和部署項目:
/t:Clean,Build /p:Configuration=UAT;DeployOnBuild=True;PublishProfile=UAT對於這三個項目之一,web.config 轉換似乎被應用了兩次,標記的元素
xdt:Transform="Insert"出現了兩次。查看建構輸出,似乎所有三個項目都執行以下目標:PreTransformWebConfig TransformWebConfigCore PostTransformWebConfig PreProfileTransformWebConfig但有問題的項目也執行這些目標(緊接上面列出的目標之後):
ProfileTransformWebConfigCore PostProfileTransformWebConfig
預設情況下,Web 項目的
.csproj文件包括以下內容:<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />該文件依次導入
\Web\Microsoft.Web.Publishing.targets,也在 VSToolsPath 下(在我的開發機器上,這對應於C:\Program Files (x86)\MSBuild\VisualStudio\v12.0)。該文件的有趣部分如下所示:
<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>雙重轉換是由於
ProfileTransformWebConfigCore執行而發生的,這是以 為條件的ProfileTransformWebConfigEnabled,只有在ProjectProfileTransformFileName和ProjectConfigTransformFileName相等時才預設為假。我將以下目標添加到我的所有三個項目中:
<Target Name="DebugWebConfigTransform" AfterTargets="PreProfileTransformWebConfig"> <Message Text="ProjectProfileTransformFileName: $(ProjectProfileTransformFileName)"/> <Message Text="ProjectConfigTransformFileName: $(ProjectConfigTransformFileName)"/> </Target>對於有問題的項目,此目標輸出以下內容:
DebugWebConfigTransform: ProjectProfileTransformFileName: Web.UAT.config ProjectConfigTransformFileName: Web.Release.config由於這兩個值不同,因此由於上述原因發生了雙重變換。
ProjectConfigTransformFilename 設置為 Web.Release.config 的原因是
ProjectConfigurationPlatforms我的.sln文件中的文件不正確。該.sln文件的配置|平台對UAT|Any CPU被映射到Release|Any CPU該項目。我認為它實際上是在應用 UAT和Release 轉換(由於我的轉換的確切性質和應用它們的順序,這與兩次應用 UAT 轉換沒有區別)。
更新
ProjectConfigurationPlatforms解決方案文件中的映射為我解決了這個問題。