.Net Core 测试项目不断丢失系统引用

本文关键字:系统 引用 Core 测试 项目 Net | 更新日期: 2023-09-27 17:55:10

我创建了一个简单的.net核心MVC Webservice。当我运行它时,它工作正常。

我添加了一个测试项目和几个测试,一切正常。

今天早上,我重构了我的 Web 服务,以便将我使用的 dao 代码移动到一个单独的程序集中。一旦我这样做,我就开始收到http500错误,我的测试项目将不再运行。

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

快速的谷歌没有找到任何东西,所以我删除了对我的dao程序集项目的引用,并将我的所有代码复制回原始项目。现在原始项目可以工作了,但我的测试项目中仍然遇到绑定错误。融合显示错误;

Test method TestService_Tests.ControllerTests.TestGetEnquiries threw exception: 
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.Assembly manager loaded from:  C:'Windows'Microsoft.NET'Framework'v4.0.30319'clr.dll
Running under executable  C:'PROGRAM FILES (X86)'MICROSOFT VISUAL STUDIO 14.0'COMMON7'IDE'COMMONEXTENSIONS'MICROSOFT'TESTWINDOW'vstest.executionengine.x86.exe
A detailed error log follows. 
Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///c:/users/matt/documents/visual studio 2015/Projects/TestService/TestService_Tests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : ReportingService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:'PROGRAM FILES (X86)'MICROSOFT VISUAL STUDIO 14.0'COMMON7'IDE'COMMONEXTENSIONS'MICROSOFT'TESTWINDOW'vstest.executionengine.x86.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:'Windows'Microsoft.NET'Framework'v4.0.30319'config'machine.config.
LOG: Post-policy reference: System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

我假设在添加 .net 核心程序集时,我在某处引起了配置问题。但是,除了删除测试项目,重新开始并希望配置问题在测试项目中之外,我如何跟踪它?

通过反复试验,我可以说错误发生在它调用控制器的行上。不测试控制器工作的虚拟测试。但是一旦对我的任何代码进行测试,就会出现问题。

{
"dependencies": {
"Microsoft.NETCore.App": {
  "version": "1.0.0",
  "type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"ReportingBusinessLayer": "1.0.0-*",
"System.Runtime": "4.1.0" },
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
  "imports": [
    "dotnet5.6",
    "portable-net45+win8"
  ]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
  "System.GC.Server": true
}
},
"publishOptions": {
"include": [
  "wwwroot",
  "Views",
  "Areas/**/Views",
  "appsettings.json",
  "web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder  publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

.Net Core 测试项目不断丢失系统引用

检查您的副本本地设置。通常,当重构后引用无法解析时,是由于复制本地在某处为 false,或者引用未添加到提供者和使用者项目中。我认为这对于这个系统库来说不是必需的,但值得检查,因为我不知道你还有什么其他框架。

此外,请确保提供程序库的目标不是与使用者不同的程序集或 .NET 运行时版本。