【问题标题】:MSBuild cannot find a referenceMSBuild 找不到参考
【发布时间】:2011-09-20 05:02:34
【问题描述】:

我目前正试图弄清楚为什么 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 框架的多个单元测试。这个项目有一个对 TenForceExecution 的引用,这是一个 Web 应用程序项目,另一个对 TenForce.Execution.Test 的引用,这是另一个包含两个项目逻辑的 DLL。

当我们在开发机器上编译所有东西时,它可以正常工作并且编译不会出错。然而,在构建代理上,这似乎失败了......

信息

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

编辑

做了一个干净的检查,Visual Studio 能够构建整个解决方案,但是 msbuild 不能。

编辑 2

检查了依赖关系:

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

编辑 3 当我删除对 webApplication 项目的引用,并将其替换为对该项目的已编译 DLL 的引用时,错误消失并且项目可以使用 MSBuild 正常编译。

编辑 4 我已经从 msbuild 通过 LOG 运行了整个构建,并查看了编译出现问题的项目的具体任务:

  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.

据我所知,实际的 WebApplication包含在参考列表中。

【问题讨论】:

    标签: c# visual-studio-2010 msbuild continuous-integration


    【解决方案1】:

    今天遇到了同样的问题,最终无法引用的项目指定了不同的平台,x86,而其他项目在 任何平台。为了解决这个问题,我必须使用 /p:Platform="Mixed Platforms" 设置参数。您可以通过转到 Configuration Manager 并查看右上角选择的 Active Solution Platform 来查看 VS 使用什么。

    【讨论】:

    • 这确实让我可以编译,但是这有点奇怪,而且最终的平台实际上变成了什么是未知的?不过,该参数值得称赞。
    • 啊,配置管理器实际上有一个混合平台的选项。很酷,msbuild 问题消失了。
    【解决方案2】:

    显然,这似乎是 MSBuild 中的一个已知问题,因为越来越多的人对此有疑问。 我还没有收到微软对此的有效回复,但讨论继续在这里:http://social.msdn.microsoft.com/Forums/en-CA/msbuild/thread/434abf1a-30db-4b13-8062-13755898dd71

    【讨论】:

    • 这个有更新吗?我现在遇到了这个问题,可以用 EDIT 3 来规避。(不理想)
    • 我在这里提交了一个错误报告:connect.microsoft.com/VisualStudio/feedback/details/770426/…
    • 我已经使用 MSBuild 了,它是有史以来最糟糕的构建系统,没有其他构建系统让我如此头疼,而且我有幸在一对夫妇上工作。对于阅读本文的任何人,请远离 MSBuild,如果您有任何其他方式可以创建您的构建,请使用它。
    • @JohnLeidegren 真相。我在其他构建系统方面有 20 年的经验,而且我从来没有像这样与构建系统抗争过。 MSBuild 实在是太烂了。
    • 现在出现“您请求的页面已被删除”。 :( ...此外,Rami 的链接出现“Microsoft Connect 已停用”。我猜互联网万岁?
    【解决方案3】:

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

    我遇到过几次这种情况,发现将目标框架从 .NET Framework 4 客户端配置文件更改为仅 .NET Framework 4 就可以了。

    【讨论】:

    • @user2609980 右键单击​​项目并点击属性。目标框架下拉菜单将位于“应用程序”选项卡上。
    • 这个修复程序今天在 VS2010 中对我有用,带有 .NET 四个和一对程序集;一个是库,另一个是控制台应用程序。该库可以很好地引用 NUnit 程序集,而控制台应用程序不能。当我将 Properties/Application 下的 Target Framework 从 .NET Framework 4 Client Profile 切换到 .NET Framework 4 时,问题就解决了。
    【解决方案4】:

    我在构建 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. 我在 msbuild 命令行中添加了“/t:restore”选项,并运行了两次 msbuild:
        msbuild mySolution.sln /p:Configuration="Release" /p:Platform="x64" /t:restore
        msbuild mySolution.sln /p:Configuration="Release" /p:Platform="x64"
    

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

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题(在 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,有时会搞砸。

      【讨论】:

        【解决方案6】:

        如果您对计算机上的新文件夹进行干净签出,它是否也可以在那里工作?

        大多数时候出现这些问题是因为您引用了本地可用但源代码管理中没有的东西。这是一个很好的检查,以查看构建项目所需的所有内容是否正确引用并处于源代码控制之下。

        【讨论】:

        • 它已经运行了将近 10 年,只有当我们开始将所有解决方案都转换为 .NET4 时,这些问题才开始......
        • 在 VS 中构建干净的签出,不在 msbuild 中构建
        • 好的,在您的问题中,我没有阅读任何有关 .net 4.0 更改的信息。您是否也更改了测试项目中的 4.0 框架?
        • 所有项目都针对 .NET 4
        【解决方案7】:

        我也遇到过从 VS 构建并不总是与简单地使用 msbuild 相同的问题。然后帮助我的是创建 msbuild 运行日志(/fl 开关)并检查日志并跟踪构建步骤并尝试找到有问题的任务。

        【讨论】:

        • 按照您的建议运行日志,并将结果添加到原始帖子中。见编辑 4
        • 通常需要整个日志来查看是否有任何问题。无论如何,您可以制作一个示例解决方案来说明这个问题吗? (因为我试图重现这个但没有成功)
        • 如果有时间我会试试的
        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

          我也遇到过同样的问题。

          我真的怀疑为什么当我们通过 MSBuild 和 Jenkins 运行解决方案文件时 MSBuild 会抛出缺少的程序集引用,但它在 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\Solution.sln" /重建

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

          注意:如果您在服务器中运行 MSBuild/Jenkins,您确实需要 Visual Studio。

          如果有任何关于此的问题,请给我发邮件............

          【讨论】:

            【解决方案10】:

            当我在发布模式下使用 MSBuild 构建解决方案时遇到此错误消息,即使调试模式工作正常并且两者都在 Visual Studio 中工作。结果在解决方案设置中,该项目未配置为针对“发布|混合平台”或“发布|任何 CPU”构建。

            【讨论】:

              【解决方案11】:

              我尝试过使用 AnyCPU 和 Prefer-32 位,但还是不行。也尝试使用 AnyCPU 而不检查 Prefer-32 位,但没有任何效果。我在使用 Bamboo 部署代码时遇到了这个问题。我确保我的包都签入到代码中(因为我在竹中的 nuget restore 有问题)并确保我在项目中的 dll 引用正在查看其路径中的包。我通过从 .gitignore 文件中删除 /packages 来签入我的包。此外,更新了我在项目中的 packages.config 文件以提及要使用的包和版本。 例如: 此外,请确保运行部署任务的代理具有所需的 .NET 框架版本。通过以上所有更改,我可以解决此参考问题。

              【讨论】:

                【解决方案12】:

                我的项目配置文件“csproj”有两个 部分和 s

                当我合并这些 部分时,此错误已消失。

                【讨论】:

                  【解决方案13】:

                  在 Visual Studio 2019 项目中遇到了同样的问题。

                  通过将项目引用从 packages.config 迁移到 PackageReference 解决了这个问题。

                  来自Microsoft Docs

                  1. 使用packages.config打开一个包含项目的解决方案。

                  2. Solution Explorer中,右键单击References节点或packages.config文件并选择Migrate packages.config to PackageReference...强>。

                  [...]

                  注意

                  在迁移开始之前,Visual Studio 会创建项目的备份,以便您在必要时回滚到 packages.config。

                  【讨论】:

                  • 我在迁移到 PackageReference 之后 开始遇到这个问题。只是说... :(
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-08-15
                  相关资源
                  最近更新 更多