简体   繁体   English

从.Net 4.6.1单元测试引用.Net标准项目时缺少方法异常

[英]Missing Method Exception When Referencing .Net Standard Project From .Net 4.6.1 Unit Test

I am getting the following exception when running a .Net 4.6.1 unit test that uses System.IO.Compression.ZipFile.Open , if the unit test project references a .Net Standard 2.0 assembly: 运行使用System.IO.Compression.ZipFile.Open的.Net 4.6.1单元测试时,如果单元测试项目引用.Net Standard 2.0程序集,则会出现以下异常:

System.MissingMethodException: Method not found: 'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.Open(System.String, System.IO.Compression.ZipArchiveMode)'.
    at UnitTestProject.UnitTest1.TestMethod1()

The unit test project was created using the VS 2017 Unit Test project (not the .NET Core one) and references were added to System.IO.Compression.FileSystem and my standard class library: 单元测试项目是使用VS 2017 Unit Test项目(而不是.NET Core one)创建的,并且引用被添加到System.IO.Compression.FileSystem和我的标准类库中:

using System.IO.Compression;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            string zipfilename = "C:\\temp\\out.zip";
            using (ZipArchive zipArchive = ZipFile.Open(zipfilename, ZipArchiveMode.Read)) { }
        }
    }

The .net standard class library is simply a single class with no methods: .net标准类库只是一个没有方法的单个类:

namespace StandardClassLib
{
    public static class Zipper
    { // Class is empty.
    }
}

I get the same error using the Test Explorer in Visual Studio and from the command line using vstest.console.exe . 我使用Visual Studio中的Test Explorer和使用vstest.console.exe从命令行获得相同的错误。

Note that this behavior only exhibits itself in a unit test project, Console Applications work fine. 请注意,此行为仅在单元测试项目中显示,控制台应用程序正常工作。

Can anyone help me understand why this isn't working and a workaround to this issue (if possible)? 任何人都可以帮助我理解为什么这不起作用并解决这个问题(如果可能的话)?

This happens because the test project needs some additional binding redirects that need to be generated during the build process. 发生这种情况是因为测试项目需要一些需要在构建过程中生成的其他绑定重定向。 While the project properties dialog has an option to auto-generate binding redirects, this has no effect for libraries (which classic unit test projects are) so you need to edit the .csproj file manually to include: 虽然项目属性对话框有一个自动生成绑定重定向的选项,但这对库(经典单元测试项目)没有影响,因此您需要手动编辑.csproj文件以包含:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

For more details and explanations, see the announcement GitHub issue Issues with .NET Standard 2.0 with .NET Framework & NuGet and its linked discussion issue. 有关更多详细信息和说明,请参阅公告GitHub问题.NET Framework 2.0与.NET Framework和NuGet的问题及其链接的讨论问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Net 4.6.1 / 4.7引用Net Standard时出错 - Error referencing Net Standard from Net 4.6.1 / 4.7 无法在引用 .Net 标准库的已发布 .Net 4.6.1 项目中加载文件或程序集“System.ComponentModel.Annotations” - Could not load file or assembly 'System.ComponentModel.Annotations' in published .Net 4.6.1 project referencing .Net Standard library 引用.NET Standard 2.0库的.NET 4.6.1项目-VSTS不会构建 - .NET 4.6.1 project referencing a .NET Standard 2.0 library - VSTS wont build .NET 4.7 xunit测试项目在引用.NET Standard 1.6项目时无法构建 - .NET 4.7 xunit test project fails to build when referencing a .NET Standard 1.6 project 在NET Standard项目中消耗.NET 4.6.1 NuGet软件包的问题(bin中缺少dll) - Problem consuming .NET 4.6.1 NuGet packages in NET Standard project ( missing dlls in bin ) 在 Dot Net 4.6.1 中运行测试项目时,SQLitePCLRaw 抛出 TypeInitializationException - When running test project in Dot Net 4.6.1, SQLitePCLRaw throws TypeInitializationException 如何在基于 .NET Framework 4.6.1 构建的单元测试项目中添加 .NET Core 2.1 项目引用 - How to add .NET Core 2.1 project reference in Unit Test project built on .NET Framework 4.6.1 从.NET Framework传统项目引用.NET Standard csproj项目 - Referencing .NET Standard csproj project from .NET Framework traditional project 从 .Net 标准类库引用 .Net Core 3.1 项目 - Referencing a .Net Core 3.1 project from a .Net Standard Class Library 从Asp.Net 4.7项目引用.Net Standard 2.0库时出错 - Error when referencing .Net Standard 2.0 library from Asp.Net 4.7 project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM