Create Nuget package from .Net Core 1 Class Library

In this tutorial we are going to see how to create a Nuget package from .Net Core 1 Class Library project. In previous versions of .Net, creating a nuget from class library is a big task which involves in adding build artifacts and dependencies. Starting from .Net Core, creating a nuget package made simpler and it is supported by Dotnet CLI.

Create a new project from Class Library (.NET Core) template in Visual Studio Community 2015 Update 3.

image

For testing purpose, create the below method in Class1.

public class Class1
{
    public string GetHello() => "Hello";
}

Build the project (F6) in Release mode. Project outputs are placed under \bin\Release\netstandard1.6 folder (Project.json has been configured to use .netstandard1.6 TFM).

Open command line prompt and navigate to the project directory. Execute the dotnet pack command as shown below.

dotnet pack –c release

 image

Now when we navigate to \bin\Release folder, we should see nupkg files generated.

image

Upload the generated package to nuget.org as shown below.

image

And on Verify Details section, click on submit to confirm package upload.

image

On successful submission, we get our package details displayed as shown below. Nuget clearly identifies the dependency of NetStandard.Library 1.6.0.

image

Now lets test the nuget with both .Net Core 1 application. I created a .Net Core 1 Console application and added our nuget as below in project.json.

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    },
    "NetCoreClassLibrary" :  "1.0.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

We can include the API in Program class as shown below.

public class Program
{
    public static void Main(string[] args)
    {
        NetCoreClassLibrary.Class1 netCoreClass = new NetCoreClassLibrary.Class1();
        Console.WriteLine(netCoreClass.GetHello());
        Console.ReadLine();

    }
}

Run the program and we see below output.

image

That’s it for now, Happy Coding and Stay Tuned!!!

You may also like...