我有一个包含三个 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
时才默认为 false和 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 的原因是我的 .sln
文件中的 ProjectConfigurationPlatforms
不正确。 .sln
文件的 UAT|Any CPU
配置|平台对已映射到此项目的Release|Any CPU
。
我认为它实际上是应用了 UAT 和释放转换作为结果(由于我的转换的确切性质以及它们应用的顺序,这与应用 UAT 转换没有区别)两次)。
更新解决方案文件中的 ProjectConfigurationPlatforms
映射解决了我的问题。
关于asp.net - Web.config 转换在发布时运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31542753/