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

一曲冷凌霜 提交于 2019-11-28 06:41:35

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/40302873/referencing-old-full-net-framework-class-library-when-targeting-net-core

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