Create And Publish A .NET Core Class Library Package Into NuGet.org

This article demonstrates how we can easily publish a class library project into nuget.org. For better understanding, we will follow step by step approach here. The approach here is to create a sample class library project and package and publish it into NuGet. Then we will use that newly created package into our another .net project and will look at how we can use the functionality that is available in that package.

Step 1 - Create a simple .Net project

Now, we will create a simple class library project in a visual studio. Then, we will go ahead and create a package out of this class.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Now, over here, let’s add a simple method for the class.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Let’s check project properties and check package property. And over here we can check all the details about the package. 

Create And Publish A .NET Core Class Library Package Into NuGet.org

Step 2 - Build and Pack your project from Visual Studio

Once the project has been built, you can right-click again on the project and click on pack.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Once Pack is complete, check debug folder of the project. You will see .nupkg created as like below.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Step 3 - Register yourself in NuGet.org and Create API Key

Now, to publish a package until you need to register with the site itself. I've already gone ahead and done this before. I registered myself with a Microsoft account. Here you can directly browse and publish the package. But here I will publish my package from the command prompt. To do that I need to generate API Key.

Create And Publish A .NET Core Class Library Package Into NuGet.org

After generation of API key in nuget.org.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Step 4 - Publish Package into nuget.org from CMD

Let’s open a command prompt and change the directory to where nupkg file is present. Execute the below command in CMD. Here, you need to use the API Key value which is created in the earlier step.

dotnet nuget push MyAwesomeLibray.1.0.0.nupkg --api-key <<API_Key >> --source https://api.nuget.org/v3/index.json

Create And Publish A .NET Core Class Library Package Into NuGet.org

Let’s check the published package in nuget.org. We can see that package is successfully published.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Step 5 - Use newly created Package into Another Project

Let’s create a console app and use the methods that are present in the NuGet package. Install this package into the project from Manage NuGet package option.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Let’s go ahead and make an object out of that class. Here, in my case Calculator object and Add method. Now go ahead and run this application and test the functionality.

Create And Publish A .NET Core Class Library Package Into NuGet.org

Awesome! We are done with the demonstration and see that how easily we can easily create a NuGet package and use them in the .net project.

Happy Learning!