MSBuild无法找到一个引用。

18

我正在尝试弄清楚为什么MSBuild无法编译我们的某个单元测试dll。问题只发生在这个DLL上,而不是其他单元测试项目。

当构建失败时,这是我从TeamCity收到的输出:

[10:38:55]: NAnt output:
[10:38:55]: [exec]
[10:38:55]: [exec]
[10:38:55]: [exec] "C:\Robinson\trunk\Projects\Robinson\Robinson.sln" (default target) (1) ->
[10:38:55]: [exec] "C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj" (default target) (19) ->
[10:38:55]: [exec] (CoreCompile target) ->
[10:38:55]: [exec] Modules\SecurityModuleTests.cs(10,30): error CS0234: The type or namespace name 'Modules' does not exist in the namespace 'TenForce.Execution.Web' (are you missing an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] Modules\SecurityModuleTests.cs(197,39): error CS0246: The type or namespace name 'SecurityModule' could not be found (are you missing a using directive or an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] TranslateTests.cs(10,30): error CS0234: The type or namespace name 'Utils' does not exist in the namespace 'TenForce.Execution.Web' (are you missing an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] IPETests.cs(8,30): error CS0234: The type or namespace name 'Ajax' does not exist in the namespace 'TenForce.Execution.Web' (are you missing an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] Pages\BasePageTest.cs(6,30): error CS0234: The type or namespace name 'Utils' does not exist in the namespace 'TenForce.Execution.Web' (are you missing an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] Pages\BasePageTest.cs(7,30): error CS0234: The type or namespace name 'Pages' does not exist in the namespace 'TenForce.Execution.Web' (are you missing an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] ServiceAsmxTests.cs(9,30): error CS0234: The type or namespace name 'Ajax' does not exist in the namespace 'TenForce.Execution.Web' (are you missing an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] ServiceAsmxTests.cs(10,30): error CS0234: The type or namespace name 'Utils' does not exist in the namespace 'TenForce.Execution.Web' (are you missing an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] Pages\BasePageTest.cs(16,17): error CS0246: The type or namespace name 'basepage' could not be found (are you missing a using directive or an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec] ServiceAsmxTests.cs(22,17): error CS0246: The type or namespace name 'Service' could not be found (are you missing a using directive or an assembly reference?) [C:\Robinson\trunk\Projects\Robinson\TenForceExecutionTests\TenForceExecutionTests.csproj]
[10:38:55]: [exec]
[10:38:55]: [exec] 2075 Warning(s)
[10:38:55]: [exec] 10 Error(s)
[10:38:55]: [exec]
[10:38:55]: [exec] Time Elapsed 00:01:40.06

项目TenForceExecutionTests是一个简单的DLL,其中包含MBunit Framework的多个UnitTests。该项目引用了TenForceExecution,这是一个Web应用程序项目,以及另一个TenForce.Execution.Test DLL,该DLL包含两个项目的逻辑。

当我们在开发机上编译所有内容时,它可以正常工作,编译也没有错误。但是在构建代理上似乎失败了...

INFO

  • 目标框架:.NET4
  • 操作系统:Windows XP 32位
  • 已安装Windows 7 SDK

EDIT

进行了干净的签出,Visual Studio能够构建整个解决方案,但msbuild无法。

EDIT 2

检查了依赖项:

  • 项目之间存在依赖关系
  • 解决方案之间存在依赖关系

EDIT 3 当我删除对Web应用程序项目的引用,并将其替换为该项目的已编译DLL的引用时,错误消失了,并且使用MSBuild编译项目时可以正常工作。

EDIT 4 我已经通过日志完全从msbuild运行整个构建,然后查看了导致问题的编译项目的特定任务:

  Task "Csc"
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\ASP.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\BUL\bin\Debug\BUL.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\BULTest\bin\Debug\BULTest.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\DAL\bin\Debug\DAL.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\Framework\bin\Debug\Framework.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\Gallio.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\Iesi.Collections.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\LanguageResource\bin\Debug\LanguageResource.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\log4net.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\MbUnit.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll" /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\NHibernate.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\PresentationBridge\bin\Debug\PresentationBridge.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\PresentationBridgeTest\bin\Debug\PresentationBridgeTest.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll" /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\System.Data.SQLite.DLL /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Extensions.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Services.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.dll" /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\TenForce.Execution.Test\bin\Debug\TenForce.Execution.Test.dll /reference:D:\Users\arne.de.herdt.TENFORCE2\Documents\RobinsonSource\Projects\Robinson\ThirdPartyComponents\WatiN.Core.dll /debug+ /debug:full /optimize- /out:obj\Debug\TenForceExecutionTests.dll /target:library Modules\SecurityModuleTests.cs TestRunSetup.cs TranslateTests.cs WebBaseTest.cs MetaTests.cs IPETests.cs Pages\BasePageTest.cs Properties\AssemblyInfo.cs ServiceAsmxTests.cs Settings.Designer.cs "D:\Users\arne.de.herdt.TENFORCE2\AppData\Local\Temp\6\.NETFramework,Version=v4.0.AssemblyAttributes.cs"
    Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
    Copyright (C) Microsoft Corporation. All rights reserved.

根据我的理解,实际的Web应用程序并没有包含在参考列表中。

13个回答

6

今天我遇到了同样的问题,最终发现无法引用的项目指定了不同的平台,x86而其他项目则是任何平台。为了解决这个问题,我必须使用/p:Platform="Mixed Platforms"设置参数。您可以通过进入配置管理器并查看右上角选择的活动解决方案平台来查看VS使用的内容。


这确实让我编译通过了,但是这种方式有点奇怪,最终的平台会变成什么样子也不太清楚?不过参数还是值得称赞的。 - Crypth
啊,配置管理器实际上有一个混合平台的选项。太酷了,msbuild问题解决了。 - Crypth

3
我在构建UWP appx包时遇到了类似的引用错误问题。有很多错误消息如下所示:
App.xaml.cs(6,15): error CS0234: The type or namespace name 'ApplicationModel' does not exist in the namespace 'Windows' (are you missing an assembly reference?)
如果使用VS2017打开sln文件,则可以无误地构建。
我采取了两个措施来解决这个问题:
1.我注意到原始解决方案需要winSDK 10.0.18362,但我的构建机器上没有它。如果使用VS2017打开,它会要求我将SDK重新定位为已安装的较旧版本。我认为msbuild可能无法进行重新定位,所以我只是下载并安装了所需的winSDK 18362。
2.我将"/t:restore"选项添加到msbuild命令行中,并运行两次msbuild:
    msbuild mySolution.sln /p:Configuration="Release" /p:Platform="x64" /t:restore
    msbuild mySolution.sln /p:Configuration="Release" /p:Platform="x64"

恢复操作,请参考https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#restore-target


3

这个问题有任何更新吗?我现在遇到了这个问题,可以通过编辑3来规避。(不是理想的解决方案) - mickyjtwin
我在这里提交了一个错误报告: https://connect.microsoft.com/VisualStudio/feedback/details/770426/msbuild-cant-find-project-reference-in-same-solution-if-it-is-not-selected-to-build-in-configuration-ex-error-cs0246-the-type-or-namespace-name-a-could-not-be-found#details - Rami A.
3
我已经受够了MSBuild,它是有史以来最糟糕的构建系统,没有其他构建系统会让我头痛如此之多,而且我曾经有幸使用过几种。对于任何阅读这篇文章的人,请远离MSBuild,如果你有任何其他方式来创建你的构建,请使用它。 - John Leidegren
5
@JohnLeidegren 真话。我有20年的其他构建系统经验,但从未像现在这样与构建系统斗争。MSBuild 简直烂透了。 - StackOverthrow
1
现在出现了“您请求的页面已被删除”的提示。:( ...此外,Rami的链接显示“Microsoft Connect已经停用”。我猜这就是互联网的好处吧? - tekHedd
该URL已经不存在了。 - Tony Dong

2

我知道这是个旧帖子,但今天我刚看到它,所以想为其他人提供反馈。

我遇到过类似情况,发现将目标框架从.NET Framework 4 Client Profile更改为.NET Framework 4就可以解决问题。


请右键单击项目并点击属性。目标框架下拉菜单将在“应用程序”选项卡上。 - r584
今天在使用VS2010和.NET四和一对程序集(一个库和另一个控制台应用程序)时,这个修复方法对我奏效了。库可以良好地引用NUnit程序集,而控制台应用程序则不行。当我在“属性/应用程序”下将目标框架从“.NET Framework 4客户端配置文件”切换到“.NET Framework 4”时,问题得到了解决。 - osullivj

2

我遇到了相同的问题(在Visual Studio中成功构建,但在MSBUILD中失败)。

尝试了这个问题中提到的不同解决方案,但都失败了。

幸运的是,最终我发现添加-tv:version可以解决这些错误。

对于我的项目,我的A.csproj中的<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>是v4.0,所以我使用了类似msbuild A.csproj -tv:4.0的命令,然后成功编译了项目。

以下是我的解决方案的结构:

  1. 项目A:启动项目和目标框架版本=v4.0

  2. 项目B:引用项目,其目标框架版本=v3.5

  3. 项目C:引用项目,其目标框架版本=v3.5

如果存在多个版本时,如果不告诉MSBUILD特定的目标框架版本,它有时会变得混乱。


0

如果您在计算机上的新文件夹中进行干净的检出,它是否也可以正常工作?

大多数情况下,这些问题是因为您引用了本地可用但不在源代码控制中的内容。这是一个很好的检查,以确保构建项目所需的所有内容都正确引用并在源代码控制下。


1
它已经运行良好将近10年了,只有当我们开始将所有解决方案转换到.NET4时,这些问题才开始出现... - codingbunny
3
在VS中进行干净的检出构建,但在msbuild中无法构建。 - codingbunny
好的,我在你的问题中没有看到任何关于 .net 4.0 更改的内容。你测试项目中的框架也已经更改成了 4.0 吗? - thekip
所有项目都针对 .NET 4。 - codingbunny

0

我尝试使用AnyCPU和Prefer-32位,但仍然无法解决问题。也尝试了使用AnyCPU而不勾选Prefer-32位,但仍然没有用。我在使用Bamboo部署代码时遇到了这个问题。我确保我的包都已经检入到代码中(因为我的nuget restore在bamboo中有问题),并确保项目中的dll引用指向其路径中的包。我通过从.gitignore文件中删除/packages来检查我的包。此外,我还更新了项目中的packages.config文件以提及要使用的包和版本。例如: 另外,我确保运行部署任务的代理程序具有所需的.NET框架版本。通过以上所有更改,我可以解决此引用问题。


0

我也遇到了同样的问题。

我真的怀疑为什么每当我们通过MSBuild和Jenkins运行解决方案文件时,它会抛出缺少程序集引用的错误,但在Visual Studio中却可以正常工作。经过两天的奋斗,我找到了一个结论。

我编写了一个批处理脚本,首先通过Visual Studio运行解决方案文件,然后再运行解决方案文件。现在它可以正常工作了。

你只需要调用批处理脚本,然后运行MSBuild/Jenkins即可。

批处理脚本:

cd C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE devenv "C:\Program Files (x86)\Jenkins\workspace\TFS\Product\Soluttion.sln" /Rebuild

devenv:在命令行中运行Visual Studio。

注意:如果你在服务器上运行MSBuild/Jenkins,你确实需要安装Visual Studio。

有任何问题,请给我发邮件..........


0

我检查了丢失的dll属性,发现程序集名称与默认命名空间名称相同。 然后我将程序集名称更改为不同的名称,解决了我的问题。


0
当我使用MSBuild在Release模式下构建解决方案时,遇到了这个错误消息,尽管Debug模式正常工作,并且在Visual Studio中两种模式都可以正常工作。原来在解决方案设置中,该项目未配置为“Release | Mixed Platforms”或“Release | Any CPU”进行构建。

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