简体   繁体   English

面向.NET Core时引用旧的(完整的.NET Framework)类库

[英]Referencing old (full .NET Framework) Class Library when targeting .NET 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 Core上开发Web应用程序,但是由于.Net Core仍在开发中,因此某些使用的库尚未在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. 我知道我可以将应用程序中的.Net 4.6定位为使用旧库,但是我不确定应用程序中使用.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 我知道这种方式使我失去了多平台功能,但实际上在我需要使用API​​和MVC统一管道,集成依赖性Ingnection容器以及其他.Net Core新功能的那一刻,我实际上并不需要它。

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. 简短而简单的答案是:您不能在.NET Core项目中引用.NET Framework 4.x库,原因很简单,因为这是两个不同的框架,而.NET 4.5 / 4.6中可用的许多API在.NET中不可用核心。

There are some exceptions though. 虽然有一些例外。

  1. If your class library does target portable-net45-win8 you can use it. 如果您的类库针对portable-net45-win8 ,则可以使用它。

    Because this profile makes sure that the API surface matches the one .NET Core uses and is based on System.Runtime . 因为此配置文件可确保API表面与.NET Core所使用的表面相匹配并且基于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 为了能够在.NET Core应用程序或.NET Core类库中导入此类包/库,您需要添加

     "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 如果您的类库的目标是dnx5x / dotnet5.x ,则可以按照上述相同的方式使用它们

  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 如果您真的很确定,则您的类库不使用任何不受支持的API,并且所有依赖项都不使用任何不受支持的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. 然后,您可以在技术上使用相同的技巧,但我建议您要这样做,因为它将允许您导入任何.NET 4.x库(甚至是2.0的库),并且您永远不会知道它是否受支持。 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 ). 而是将您的类库转换为可移植类库,并以netstandard1.x目标(取决于之前使用的.NET Framework版本,请参见此矩阵 )。

    For example, if your class library was targeting .NET Framework 4.5.1 before, then convert it to netstandard1.2 . 例如,如果您的类库以前是针对.NET Framework 4.5.1的,则将其转换为netstandard1.2 If there are any errors about missing API, then it was using APIs not supported by .NET Core. 如果存在有关缺少API的任何错误,则表明它使用的是.NET Core不支持的API。 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. 您必须解决此问题(通过使用#if !NETSTANDARD1_2 precompiler指令从netstandard1.2版本中删除它,或者尝试使用更高的netstandard版本。您还可以在新的project.json缺少的类库中定位两个目标。

Yes, it's possible. 是的,有可能。 I have a project targeting NHibernate (that depends on full .NET Framework) and also some old format Class Library projects (csproj). 我有一个针对NHibernate的项目(取决于完整的.NET Framework),还有一些旧格式的类库项目(csproj)。

I use DI and MVC/API without problems. 我使用DI和MVC / API没有问题。

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

相关问题 面向完整框架的ASP.NET Core应用-InProcess IIS? - ASP.NET Core app targeting full framework — InProcess IIS? 面向完整框架的.NET Core应用-停止在IIS中工作 - .NET Core app targeting full framework - Stopped working in IIS 在一个Nuget包中定位.Net核心框架和完整的.Net 4.5 / 4.6框架 - Targeting .Net Core Framework and Full .Net 4.5/4.6 Framework in one Nuget Package 在针对.NET Core的类库包中使用JsonConvert.DeserializeObject - Using JsonConvert.DeserializeObject in a Class Library Package targeting .NET Core asp.net 核心针对 .net 框架 - asp.net core targeting .net framework 如何在ASP.NET Core(以.NET Framework为目标)项目中引用旧程序集? - How to reference an old assembly in ASP.NET Core (.NET Framework targeting) project? 从类库(.NET Core)引用ASP.NET Core Web应用程序(.NET Core) - Referencing ASP.NET Core Web Application(.NET Core) from Class Library (.NET Core) 如果 ASP.NET 5 被重命名为 ASP.NET Core 1.0,那么针对完整 .NET 框架的新 ASP.NET 项目是什么? - If ASP.NET 5 is being renamed ASP.NET Core 1.0, what is a new ASP.NET project targeting full .NET framework? 在MAC上针对Core和Full .NET构建.NET Core - Build .NET Core targeting both Core and Full .NET on MAC 在类库项目中引用ASP.NET Core应用程序 - Referencing a ASP.NET Core application in Class Library project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM