Referencing old (full .NET Framework) Class Library when targeting .NET Core

隐身守侯 提交于 2019-11-29 12:58:53

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.

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

I use DI and MVC/API without problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!