[英]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. 虽然有一些例外。
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" ] } }
dnx5x
/ dotnet5.x
, then they can be used with the same way as the above dnx5x
/ dotnet5.x
,则可以按照上述相同的方式使用它们 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.