【问题标题】:Missing project dependency when referring project引用项目时缺少项目依赖项
【发布时间】:2017-07-19 14:34:10
【问题描述】:

在 Visual Studio 中引用项目时,我遇到了一些依赖项问题。以下是我的解决方案 ReferenceTest 的结构:

  • Common. 包含返回字符串的静态 CommonClass.HelloWorld() 方法的类库。此方法返回的字符串是使用使用 NuGet 安装的 Microsoft.Extensions.Configuration(及其大量依赖项)从 JSON 配置文件中读取的。
  • ConsoleApplication1. 控制台应用程序使用静态 Worker.DoWork() 方法将 CommonClass.HelloWorld() 字符串写入控制台。此控制台应用程序具有对 Common 项目的项目引用。
  • ConsoleApplication1Test. 一个使用 NUnit 的类库,用于测试 ConsoleApplication1 中的 Worker.DoWork() 方法是否返回了预期的字符串。这个类库有一个对 ConsoleApplication1 项目的项目引用。

ConsoleApplication1 控制台应用程序按预期工作,但是在 ConsoleApplication1Test 中运行单元测试时,我得到了这个异常:

System.IO.FileNotFoundException:无法加载文件或程序集 'System.Runtime,版本=4.1.1.0,文化=中性, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。这 系统找不到指定的文件。

System.Runtime.dll 文件(可能还有其他文件)在编译 ConsoleApplication1Test 项目时不会复制到 bin 文件夹中。为什么会这样?

可在此处找到包含演示解决方案的 zip 文件: http://www.filedropper.com/referencetest

【问题讨论】:

  • 我认为您的“测试”应用程序应该具有与您的主项目类似的参考。因此,您的 ConsoleApplication1Test 应该有对 Common 的引用。添加此引用时,它是否按预期工作?
  • MsTest 有一个[DeploymentItem("System.Runtime.dll")] 属性来强制测试运行器将某个文件复制到其测试文件夹,不确定是否有与 NUnit 等效的东西。
  • ConsoleApplication1Test 中添加对 Common 的引用不起作用。
  • 您可以发布项目/解决方案文件的minimal reproducible example 吗? (我很好奇解决方案文件中实际上是如何引用该文件的)。

标签: c# .net nunit


【解决方案1】:

解决方案

我能够重现并解决此问题,并生成说明差异的构建日志。

首先,解决方案。我注意到 ConsoleApplication1.csproj 文件有一行测试项目没有的配置。所以,我补充说:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

到测试项目文件。第一个 &lt;PropertyGroup&gt; 部分现在如下所示:

<PropertyGroup>
  <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
  <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
  <ProjectGuid>{A97E82A2-2EF9-43AB-A46B-882131BAF1D0}</ProjectGuid>
  <OutputType>Library</OutputType>
  <AppDesignerFolder>Properties</AppDesignerFolder>
  <RootNamespace>ConsoleApplication1Test</RootNamespace>
  <AssemblyName>ConsoleApplication1Test</AssemblyName>
  <TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
  <FileAlignment>512</FileAlignment>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

现在单元测试失败,因为它找不到 config.json。成功!

编辑:从下面的命令行运行构建后,单元测试通过。我不确定为什么从 Visual Studio 构建时 config.json 不存在。

部分解释

这个AutoGenerateBindingRedirects 属性似乎改变了构建过程解析对属于 .NET Framework 的库的引用的方式。例如,详细日志输出之前和之后的比较表明:

Unified Dependency "System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". (TaskId:97)
    Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Configuration.Abstractions.dll" because there is a more recent version of this framework file. (TaskId:97)
    Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Configuration.dll" because there is a more recent version of this framework file. (TaskId:97)
    Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Configuration.Binder.dll" because there is a more recent version of this framework file. (TaskId:97)
    Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Primitives.dll" because there is a more recent version of this framework file. (TaskId:97)
    Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.FileProviders.Abstractions.dll" because there is a more recent version of this framework file. (TaskId:97)
    Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\System.Runtime.CompilerServices.Unsafe.dll" because there is a more recent version of this framework file. (TaskId:97)
    Resolved file path is "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7\Facades\System.Runtime.dll". (TaskId:97)
    Reference found at search path location "{TargetFrameworkDirectory}". (TaskId:97)

更改为:

Unified Dependency "System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". (TaskId:97)
  Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Configuration.Abstractions.dll" because AutoUnify is 'true'. (TaskId:97)
  Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Configuration.dll" because AutoUnify is 'true'. (TaskId:97)
  Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Configuration.Binder.dll" because AutoUnify is 'true'. (TaskId:97)
  Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.Primitives.dll" because AutoUnify is 'true'. (TaskId:97)
  Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\Microsoft.Extensions.FileProviders.Abstractions.dll" because AutoUnify is 'true'. (TaskId:97)
  Using this version instead of original version "4.0.0.0" in "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\System.Runtime.CompilerServices.Unsafe.dll" because AutoUnify is 'true'. (TaskId:97)
  Resolved file path is "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug\System.Runtime.dll". (TaskId:97)
  Reference found at search path location "C:\Users\User\Desktop\ReferenceTest\ConsoleApplication1\bin\Debug". (TaskId:97)

我想 app.config 文件中的程序集绑定重定向会在构建过程中影响程序集引用路径解析的某些方面。仅在添加指定属性后,此构建输出的外观才支持这一点:

Added Item(s): 
    _ResolveAssemblyReferencesApplicationConfigFileForExes=
        app.config
                OriginalItemSpec=app.config
                TargetPath=ConsoleApplication1Test.dll.config

我以前没有见过这个特定的属性,我不知道为什么它会包含在某些项目中而不包含在其他项目中,或者是否有 UI 可以更改此设置。

作为参考,为了生成上面的构建输出比较,我执行了以下操作:

  1. 从问题中提供的链接加载项目
  2. 将 NUnit3TestAdapter NuGet 包添加到 Test 项目(个人偏好 - 使用 VS 测试运行器时出现错误)
  3. 运行测试以验证错误
  4. 清洁溶液
  5. 从解决方案文件夹中的开发人员命令提示符运行 msbuild /verbosity:diag ReferenceTest.sln &gt; build.txt
  6. 如上所述修改测试项目
  7. 运行msbuild /verbosity:diag ReferenceTest.sln &gt; build2.txt
  8. 运行devenv /diff build.txt build2.txt 或您最喜欢的比较工具

【讨论】:

    【解决方案2】:

    看来您从 Common 引用的 Newtonsoft.Json 库本身正在引用 System.Runtime 4.0 版 但是您所有的项目都针对 4+ 框架。 这就是冲突点。

    尝试使用 Newtonsoft.Json 库升级或重新安装 NuGet 包或将所有项目的目标框架降级到 4.0 版。

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多