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

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

问题描述

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



I知道我可以将应用程序中的.Net 4.6定位为使用旧库,但是我不确定应用程序中使用.Net Core新功能的实际变化。



我知道这种方式使我失去了多平台功能,但实际上在我需要使用API​​和MVC统一管道,集成依赖项Ingnection容器以及其他.Net Core新功能的那一刻,我实际上并不需要它。



通过定位旧框架是否有可能实现这一目标?

解决方案

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



尽管有一些例外。


  1. 如果您的类库确实定位了 portable-net45-win8 ,可以使用它。



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

     框架: {
    netcoreapp1.0:{
    依赖关系:{},
    进口:[ dotnet5.6, portable-net45-win8]
    }
    }


  2. 如果类库的目标是 dnx5x / dotnet5.x ,则可以按照与上述相同的方式使用它们。


  3. 如果您真的很确定,则您的类库不会使用 ANY 不支持的API,并且不会使用 none 它的依赖项使用 ANY 不支持的API



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



    相反,请转换您的课程将该库导入可移植类库,并定位 netstandard1.x (取决于以前使用的.NET Framework版本,请参见此矩阵)。



    例如,如果您的类库是之前定位.NET Framework 4.5.1,然后将其转换为 netstandard1.2 。如果存在有关缺少API的任何错误,则表明它使用的是.NET Core不支持的API。您必须解决此问题(使用 #if!NETSTANDARD1_2 precompiler指令将其从 netstandard1.2 版本删除或尝试使用更高的netstandard版本。您还可以在新的project.json缺少的类库中定位两个目标。



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.

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.

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?

解决方案

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.

There are some exceptions though.

  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" ] 
      }      
    }
    

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

  3. 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.

    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).

    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天全站免登陆