在标准 .net 控制台应用程序中使用 .Net Core dll [英] Using .Net Core dll in standard .net Console app

查看:38
本文介绍了在标准 .net 控制台应用程序中使用 .Net Core dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 .NET Core 2.1 在 VS 2017 中构建了一个类库.现在,我想在 .NET 框架 4.6.2 中内置的标准控制台应用程序中使用该 dll.

I have built a class library in VS 2017 using .NET Core 2.1. Now, I want to use that dll in a standard console app built in .NET framework 4.6.2.

当我在控制台应用程序中引用 .NET Core dll 时,我收到此错误:

When I refer to the .NET Core dll in the console app, I am getting this error:

未处理的异常:System.IO.FileLoadException:无法加载文件或程序集 'System.Runtime, Version=4.2.1.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一.这定位的程序集的清单定义与程序集不匹配参考.(来自 HRESULT 的异常:0x80131040)在Marketo_Console.Program.Main(String[] args)

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at Marketo_Console.Program.Main(String[] args)

如何解决这个问题?如何在标准控制台应用中使用 .NET Core dll?

How to resolve this? How can I use a .NET Core dll in a standard console app?

推荐答案

如何解决这个问题?

How to resolve this?

你不能.至少不是直接的.

You cannot. At least not directly.

  • .NET Core 可以运行面向 .NET Core 本身或 .NET Standard 的程序集
  • .NET Framework 可以运行面向 .NET Framework 本身或 .NET Standard 的程序集

因此无法将分别针对 Full Framework 和 Core 的两个程序集放在一起.不在核心中,也不在完整的框架中.(您可能可以在各处找到 hack 和但对我来说这行得通"的解决方案,但对于它们的特定用例,它们是非常脆弱的例外.

So there is no way to bring two assemblies that target Full Framework and Core respectively together. Not in Core and not in full framework. (You might be able to find hacks and "but for me this works" solutions all over the place, but they are super brittle exceptions for their specific use case).

可以将 .NET Core 程序集的目标框架切换到 .NET Framework 或 .NET Standard,或者您可以提供多个目标框架来构建支持 .NET Core 完整框架的 Nuget 包.

You could switch your target framework of the .NET Core assembly to either .NET Framework, or to .NET Standard, or you could give multiple target frameworks to build a Nuget package that supports .NET Core and full framework.

但是如果您需要一个 .NET Core 程序集一个 .NET Framework 应用程序,则不能以这种方式将它们一起使用.

But if you need a .NET Core assembly and a .NET Framework application, you cannot use them together that way.

这篇关于在标准 .net 控制台应用程序中使用 .Net Core dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆