引用项目时缺少项目依赖性

时间:2017-07-11 14:50:05

标签: 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:无法加载文件或程序集   ' System.Runtime,Version = 4.1.1.0,Culture = neutral,   公钥= b03f5f7f11d50a3a'或其中一个依赖项。该   系统找不到指定的文件。

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

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

2 个答案:

答案 0 :(得分:8)

解决方案

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

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

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

到Test项目文件。第一个<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文件中的程序集绑定重定向正在影响构建过程中程序集引用路径解析的某些方面。只有在添加指定的属性后,此构建输出的外观才支持此功能:

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

之前我还没有看过这个特定的属性,我不知道为什么它会被包含在某些项目中而不包含在其他项目中,或者如果有用户界面来改变这个设置。

作为参考,为了产生上面的构建输出比较,我做了以下几点:

  1. 从问题
  2. 中提供的链接加载项目
  3. 将NUnit3TestAdapter NuGet包添加到Test项目(个人首选项 - 使用VS测试运行时出现错误)
  4. 运行测试以验证错误
  5. 清洁解决方案
  6. 从解决方案文件夹
  7. 中的Developer Command Prompt运行msbuild /verbosity:diag ReferenceTest.sln > build.txt
  8. 如上所述修改测试项目
  9. 运行msbuild /verbosity:diag ReferenceTest.sln > build2.txt
  10. 运行devenv /diff build.txt build2.txt或您最喜爱的比较工具

答案 1 :(得分:1)

您从Common引用的Newtonsoft.Json库似乎是通过itselft引用到System.Runtime ver 4.0 但是您的所有项目都针对4+框架。 这就是冲突点。

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