在 .NET 4.5.2 控制台应用程序中使用 .NET Core 库 [英] Using .NET Core library in .NET 4.5.2 console application

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

问题描述

我有两个项目:一个项目基于 .NET Core,另一个项目基于普通的 .NET Framework 4.5.2.

I have two projects: one project is built on .NET Core and the other one is built on normal .NET Framework 4.5.2.

我想知道如何在我的 .NET 控制台应用程序中使用我的 .NET Core cass 库.

I want to know how I can use my .NET Core cass library in my .NET Console Application.

这是我的 project.json:

Here is my project.json:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "NLog.Web.AspNetCore": "4.3.0",
    "System.Xml.XmlSerializer": "4.0.11"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    },
    "net452": {}
  }
}

我尝试将该项目作为普通参考.但是在重新构建解决方案后,控制台应用程序仍然没有获得 .NET Core 库作为参考.

I tried to include the project as a normal reference. But after re-building the solution the Console Application still doesn't get the .NET Core library as a reference.

推荐答案

Visual Studio 2017 本身就可以做到这一点;Visual Studio 2015 不能(从更新 3 开始).许多这些工具错误和问题正在通过 .NET Core 工具的稳定版本得到修复和最终确定,预计会在 VS 2017 中出现.

Visual Studio 2017 can do this natively; Visual Studio 2015 cannot (as of Update 3). A lot of these tooling bugs and issues are being fixed and finalized with the stable version of the .NET Core tooling, expected with VS 2017.

如果您仍在使用 VS 2015,今天可以使用两种解决方法:

There are two workarounds you can use today, if you are still using VS 2015:

  • 使用 dotnet pack 为类库生成 NuGet 包.由于您在 project.json 中定位 net452,它将与 .NET Framework 4.5.2 项目兼容.您可以将其托管在 本地提要 上,并将其安装在您的控制台应用程序项目中.

  • Use dotnet pack to produce a NuGet package for the class library. Since you target net452 in your project.json, it will be compatible with a .NET Framework 4.5.2 project. You can host it on a local feed and install it in your console application project.

手动引用编译库时生成的 DLL.它应该位于 bin/net452/YourLibrary.dll.这不是一个很好的解决方案,因为它很脆,很容易折断.

Manually reference the DLL that's produced when you compile the library. It should be at bin/net452/YourLibrary.dll. This isn't a great solution because it's brittle and can break easily.

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

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