MSBuild编译具有项目依赖关系的解决方案失败

3

我的解决方案中包含多个项目,构建突然出现故障,并且无法确定问题的原因。解决方案中的所有项目都针对相同的 .NET Framework 版本(4.5.1),但其中某些项目的依赖项可能针对较早版本的框架。上周我们还能成功构建此类情况下的解决方案。从上周四开始,生成似乎已经出现了故障,但是构建服务器或解决方案并没有进行任何更改。

为了说明和消除我们生产环境设置的一些复杂性,我创建了一个示例解决方案,模仿这种行为。

项目结构:

  • CBI 解决方案
    • CBI 网站
    • CBI 实现库
    • CBI 核心库,定义接口

网站和类库的目标框架版本均为 4.5.1。

重现步骤

解决方案 1

Github 代码库:https://github.com/NikitaGolovko/Solution1.git

使用 MSBuild 14 进行构建,我的命令是:

msbuild.exe "%SOLUTION1PATH%\CBILite.sln" /p:platform="Any CPU" /p:configuration="Release" /v:d

输出

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v /localhost_61806 -p cbi\ -u -f PrecompiledWeb\lo
  calhost_61806\
  Microsoft (R) ASP.NET Compilation Tool version 4.7.2046.0
  Utility to precompile an ASP.NET application
  Copyright (C) Microsoft Corporation. All rights reserved.


Done executing task "AspNetCompiler".
Task "Message" skipped, due to false condition; (!((false) or ('$(AspNetConfiguration)' == 'Debug') or ('$(AspNetConfig
uration)' == 'Release'))) was evaluated as (!((false) or ('Release' == 'Debug') or ('Release' == 'Release'))).
Done building target "Build" in project "cbi.metaproj".
Done Building Project "D:\Work\DotNet\Nikita\Solution1\cbi.metaproj" (default targets).

Done executing task "MSBuild".
Done building target "Build" in project "CBILite.sln".
Done Building Project "D:\Work\DotNet\Nikita\Solution1\CBILite.sln" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.10

完整输出结果请参见此处:https://github.com/NikitaGolovko/Solution1/blob/master/MSBuild_Output.txt

Solution 2

Github代码库:https://github.com/NikitaGolovko/Solution2.git

该解决方案与Solution1几乎相同,唯一的区别是在CBI Implementation库中增加了Unity的依赖项。为了简化流程和消除nuget恢复步骤,我已将nuget包包含在解决方案中。

使用MSBuild 14进行构建。我的命令是:

msbuild.exe "%SOLUTION2PATH%\CBILite.sln" /p:platform="Any CPU" /p:configuration="Release" /v:d

输出

  C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v /localhost_61806 -p cbi\ -u -f PrecompiledWeb\lo
  calhost_61806\
  Microsoft (R) ASP.NET Compilation Tool version 4.7.2046.0
  Utility to precompile an ASP.NET application
  Copyright (C) Microsoft Corporation. All rights reserved.

D:\Work\DotNet\Nikita\Solution2\cbi\Index.aspx.vb(5): error BC30002: Type 'CBILite.Calculator' is not defined. [D:\Work
\DotNet\Nikita\Solution2\cbi.metaproj]
  The command exited with code 1.

Done executing task "AspNetCompiler" -- FAILED.
Done building target "Build" in project "cbi.metaproj" -- FAILED.
Done Building Project "D:\Work\DotNet\Nikita\Solution2\cbi.metaproj" (default targets) -- FAILED.

Done executing task "MSBuild" -- FAILED.
Done building target "Build" in project "CBILite.sln" -- FAILED.
Done Building Project "D:\Work\DotNet\Nikita\Solution2\CBILite.sln" (default targets) -- FAILED.


Build FAILED.

完整的输出结果在此处:https://github.com/NikitaGolovko/Solution2/blob/master/MSBuild_Output.txt

观察结果

Bin文件夹

查看网站的bin文件夹时,我发现在Solution2构建后CBILite.dll文件丢失了,但在Solution1中存在。

TargetFrameworkVersion

在MSBuild参数中传递TargetFrameworkVersion似乎不会影响构建。我尝试传递4.5、4.5.1都没有成功。然而传递3.5会导致构建成功。这非常奇怪。

Metaproj

比较生成的两个解决方案的Metaproj文件时,唯一可以观察到的并且似乎很重要的区别是缺少TargetFrameworkVersion元素。没有依赖项的Solution1包含v.4.0元素。有依赖项的Solution2则没有。 我没有在代码库中包含metaproj文件,但如果需要可以共享它们。

Visual Studio

在Visual Studio中构建解决方案完全没有问题。

其他想法

问题突然出现,并似乎影响到我们大部分包含混合项目(C#/VB + 网站)的解决方案。Web项目继续正常运行。

我尝试使用MSBuild 15,但结果相同。

有一些解决方法:

  1. 保留metaproj文件并手动修改它,添加v4.5.1元素。
  2. 添加一个额外的构建步骤,通过批处理文件或其他方式手动将CBILib.dll复制到网站项目中。
  3. 为网站添加指向CBILib/bin/Release文件夹的刷新文件。

虽然所有这些解决方案都可能有效,但它们肯定是hack,将来会带来问题。

我希望其他人能提供更好的建议来解决这个问题。

1个回答

0
请确保您已安装以下 NuGet 包:
  • Microsoft.NET.Test.Sdk

  • MSTest.TestAdapter

  • MSTest.TestFramework

  • Microsoft.NETCore.App(如果您使用 .NET Core)


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