Reference .NET 4.5 dll in .NET Core 1.1 csproj?

前端 未结 2 1842
陌清茗
陌清茗 2020-12-31 05:50

I\'m running VS 2017 RC4.

I add a reference in my .NET Core app to my .NET 4.5 dll and it compiles. When a line that references the dll is called at runtime, I get:<

2条回答
  •  囚心锁ツ
    2020-12-31 06:38

    You can't (safely) load a .NET Framework 4.5 library into .NET Core, because it may use APIs which are unavailable in .NET Core.

    Only if your library targets portable-net45+win8 (.NET Framework 4.5 and Windows 8 Portable Class Profile or higher) it can be used with .NET Core. Because this specific PCL Profile limits the API which is compatible to (what was formerly called WinRT) System.Runtime, which is what is .NET Core is based on.

    For a list of compatible PCL profiles, see this list (PCL Compatibility at the bottom)

    If the assembly you want to reference do not support netstandard1.x or any of the supported profiles, you have to target .NET Framework 4.5 instead of .NET Core.

    In your csproj

    net45
    ...
    
        
    

    or if you multi-target

    net45;netcoreapp1.1
    ...
    
        
        
    

    You just can't auto-magically use any .NET Framework 4.5 library in your .NET Core project. Only PCL and netstandard1.x ones.

    Update

    For the sake of completeness:

    If you are certain that your class library/package targets a compatible PCL, you can make nuget restore this packages too, even if they don't target netstandard1.x.

    
        $(PackageTargetFallback);portable-net45+win8+wp8+wpa81;
    
    

    Word of warning

    Never, I repeat, NEVER put anything else there except for compatible PCL Libraries. NEVER put net45 in here. This will just force NuGet to download and install this package, but it won't make it work and crash at runtime with similar error like you have above!

    It's only there to force nuget to install package which are know to work with .NET Core for the transition period until most packages target netstandard1.x!

提交回复
热议问题