Using .NET Core packages on legacy .NET platforms

2020-07-20 03:43发布

问题:

I'm creating a NuGet package using one of those new "Class Library (Package)" templates in VS. I want to know, is there any way I can use my package from a PCL that does not use .NET Core?

For example, I have a package, Enu, with the following project.json:

{
    "title": "Enu",
    // project details...

    "dependencies": {
        "System.Runtime": "4.0.0"
    },

    "frameworks": {
        "dotnet": { }
    }
}

Then I have a legacy PCL that targets .NET 4.5, Windows 8, and Windows 8.1. When I try to install the package via Install-Package Enu, I get the following error:

PM> Install-Package Enu
# blah blah blah
Install-Package : Could not install package 'Enu 4.0.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain 
any assembly references or content files that are compatible with that framework. For more information, contact the package author.

What can I do to fix this?

note: Weirdly enough, it works on a console application.

回答1:

Yep, it's possible. Here's a nice graph for reference (taken from here):

tl;dr To target everything add this to your project.json:

"frameworks": {
    "dotnet": { }, // .NET 4.6 and DNX
    "net45": { }, // .NET Framework 4.5
    "netcore45": { } // WinRT and Silverlight
}