面向 .NET Core 时引用旧的(完整的 .NET Framework)类库 [英] Referencing old (full .NET Framework) Class Library when targeting .NET Core

查看:82
本文介绍了面向 .NET Core 时引用旧的(完整的 .NET Framework)类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 .Net Core 上开发 Web 应用程序,但由于 .Net Core 仍在开发中,一些使用的库尚未构建在 Core 上.

I'm developing a web application on .Net Core, but as .Net Core is still on development some libraries used are not built on Core yet.

我知道我可以在我的应用程序中以 .Net 4.6 为目标来使用旧库,但我不确定在我的应用程序中使用新的 .Net Core 功能有什么实际变化.

I know I can target .Net 4.6 in my application to use old libraries but I'm not sure about what actually change in the use of new .Net Core functionality inside my app.

我知道这样我会失去多平台功能,但实际上我现在不需要它,因为我需要继续使用 API 和 MVC 统一管道、集成依赖项 Ingnection 容器和其他 .Net Core 新功能

I know this way I'm loosing multi platform capabilities but I actually don't need it at the moment insead I need going on using API and MVC unified pipeline, the integrated dependency Ingnection container and other .Net Core new functionality

以旧框架为目标是否可以实现?

Will this be aniway possible by targeting old framework?

推荐答案

简短而简单的答案是:您不能在 .NET Core 项目中引用 .NET Framework 4.x 库,原因很简单,因为它是两个不同的框架.NET 4.5/4.6 中可用的许多 API 在 .NET Core 中不可用.

The short and simple answer is: You can't reference .NET Framework 4.x libraries in .NET Core project for the simple reason that it's two different frameworks and many APIs that were available in .NET 4.5/4.6 are unavailable in .NET Core.

不过也有一些例外.

  1. 如果您的类库确实针对 portable-net45-win8,您可以使用它.

    因为此配置文件确保 API 表面与 .NET Core 使用的一个匹配并且基于 System.Runtime.为了能够在 .NET Core 应用程序或 .NET Core 类库中导入此类包/库,您需要添加

  1. If your class library does target portable-net45-win8 you can use it.

    Because this profile makes sure that the API surface matches the one .NET Core uses and is based on System.Runtime. In order to be able to import such packages/libraries in .NET Core application or .NET Core class library, you need to add

"frameworks:" {
  "netcoreapp1.0": {
    "dependencies : { },
    "imports": [ "dotnet5.6", "portable-net45-win8" ] 
  }      
}

  • 如果你的类库针对的是dnx5x/dotnet5.x,那么它们的使用方式和上面一样

  • 如果你真的很确定,你的类库不使用任何不受支持的 API 和没有它的依赖项使用任何不受支持的 API

    然后您可以在技术上使用相同的技巧,但我建议永远不要这样做,因为它允许您导入任何 .NET 4.x 库(甚至 2.0 库)并且您'永远不知道它是否支持.所以不要这样做永远.

  • If your class library targets dnx5x / dotnet5.x, then they can be used with the same way as the above

  • If you are really really sure, your class library don't use ANY unsupported API and none of it's dependencies use ANY unsupported API

    Then you could technically use the same trick, but I would advice to NEVER do this, as it will allow you to import any .NET 4.x library (even 2.0 ones) and you'll never know if its supported or not. SO DON'T DO THIS EVER.

    相反,将您的类库转换为可移植类库并以 netstandard1.x 为目标(取决于之前使用的 .NET Framework 版本,请参阅此 矩阵).

    Instead, convert your class library into a Portable Class library and target netstandard1.x (depends on the .NET Framework version used before, see this matrix).

    例如,如果您的类库之前面向 .NET Framework 4.5.1,则将其转换为 netstandard1.2.如果有任何关于缺少 API 的错误,那么它使用了 .NET Core 不支持的 API.您必须解决该问题(使用 #if !NETSTANDARD1_2 预编译器指令将其从 netstandard1.2 版本中删除,或尝试使用更高的 netstandard 版本.您也可以针对两个目标在新的 project.json absed 类库中.

    For example, if your class library was targeting .NET Framework 4.5.1 before, then convert it to netstandard1.2. If there are any errors about missing API, then it was using APIs not supported by .NET Core. You'll have to fix that (remove it from the netstandard1.2 version by using #if !NETSTANDARD1_2 precompiler directive or try a higher netstandard version. You can also target two targets in the new project.json absed Class libraries.

    这篇关于面向 .NET Core 时引用旧的(完整的 .NET Framework)类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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