c# - 引用项目时缺少项目依赖项

标签 c# .net nunit

在 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 : Could not load file or assembly 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

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

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

最佳答案

解决方案

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

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

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

到测试项目文件。第一个<PropertyGroup>部分现在看起来像这样:

<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 文件中的程序集绑定(bind)重定向会在构建过程中影响程序集引用路径解析的某些方面。此构建输出的外观支持这一点,仅在添加指定属性后:

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

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

作为引用,为了生成上面的构建输出比较,我做了以下操作:

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

关于c# - 引用项目时缺少项目依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45037920/

相关文章:

c# - 为什么 SQL float 与 C# float 不同

c# - 不同值类型之间的除以零行为不一致

unit-testing - 如何在 Rider 中按类别运行单元测试?

visual-studio - Microsoft是否有计划支持MSTest以外的测试框架?

c# - SQL Server 2008 R2 连接字符串

c# - 在 Blazor 中使用 InputFile 选择的绑定(bind)文件

C# 更新 mySQLDateTime 数据表

c# - 将 XML 文件读入 TreeView

c# - 在库中使用 .Net Standard 1.4 并在应用程序中使用 .Net framework 4.6.1 时,无法加载文件 System.IO.FileSystem,Version=4.0.1.0

c# - 在 NUnit 中,如何指示 'DataPoint' 仅适用于一种理论?