Web.config转换在发布时运行两次

10

我有一个解决方案,包括三个Web项目(以及许多类库项目)。所有Web项目都使用Web.config变换来指定每个环境的配置。

我有针对多个构建配置文件的Web.config变换,分别命名为Web.UAT.config,Web.Staging.config和Web.Release.config。

我正在使用以下参数从我的CI服务器使用MSBuild构建和部署项目:

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

对于这三个项目中的一个,web.config转换似乎被应用了两次,标记为xdt:Transform="Insert"的元素出现了两次。查看构建输出,似乎所有三个项目都运行以下目标:

PreTransformWebConfig
TransformWebConfigCore
PostTransformWebConfig
PreProfileTransformWebConfig

但这个问题项目也运行这些目标(紧接在上述列出的目标之后):
ProfileTransformWebConfigCore
PostProfileTransformWebConfig
4个回答

8

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, 只有当ProjectProfileTransformFileNameProjectConfigTransformFileName相等时默认为false。
我在所有三个项目中添加了以下目标:
  <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文件的Configuration|Platform对UAT|Any CPU被映射到该项目的Release|Any CPU。
我认为实际上它会应用UAT和Release转换(由于我的转换的确切性质以及它们被应用的顺序,这与应用UAT转换两次是无法区分的)。
更新解决方案文件中的ProjectConfigurationPlatforms映射解决了我的问题。

1
这个问题出现是因为我的解决方案配置中有多个项目使用了不同的配置。由于这种配置,它运行了多个web.config变换。

Configuration Manager

在将项目切换为使用相同的配置后,我不再收到web.config中的问题。

0

如果您没有添加Configuration msbuild参数,这种情况似乎也会发生。PublishProfile并不足够好。


0

我的问题与所有答案都不同。在我的情况下,我有一个名为staging.pubxml的配置文件,它使用了prod配置。在发布时,会发生stagingprod转换。

事实证明,如果可以找到相同的配置,.pubxml文件的名称也会触发转换。我只需更改文件名即可解决我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接