developer82
developer82

Reputation: 13723

Add .NET Core support to existing class library

I have an existing open source library that I have written in .NET 4.5 that I want to create a NuGet from. The problem is that this NuGet package will only be available for .NET 4.5 applications.

What I want to do is have support for both .NET 4.5 and .NET Core on the same NuGet (I already seen packages that do that line JSON.NET) - how can I add support to an existing NuGet? And how do I support in a single class library to multiple .NET versions?

Upvotes: 1

Views: 167

Answers (1)

Vitaliy Fedorchenko
Vitaliy Fedorchenko

Reputation: 9235

You may keep using csproj for net45 target and add project.json that targets both "net45" and "netstandard1.x" frameworks (use project.json of my library as a sample):

  "frameworks": {
    "net45": {
      "frameworkAssemblies": {
        "System.Data": "",
        "System.ComponentModel.DataAnnotations" : ""
      },
      "buildOptions": {
        "define": []
      }
    },
    "netstandard1.5": {
      "dependencies": {
        "NETStandard.Library": "1.6.0",
        "System.Data.Common": "4.1.0",
        "System.Reflection": "4.1.0",
        "System.Reflection.Primitives": "4.0.1",
        "System.Threading": "4.0.11",
        "System.Threading.Tasks": "4.0.11",
        "System.ComponentModel.Annotations": "4.1.0" 
      },
      "buildOptions": {
        "define": [ "NET_STANDARD" ]
      }
    }

Note that you can define conditional compilation constants for net45 or netcore specific code snippets.

When you have project.json, you may prepare nuget package that includes both net45 and netstandard builds with the following command:

> dotnet pack --configuration Release

Don't forget that "dotnet pack" doesn't use information from nuspec file and all metadata should be present in project.json.

Upvotes: 1

Related Questions