Create and Publish NuGet Package using Visual Studio

Create NuGet package visual studio 2019

In this article, we will see How to create NuGet package visual studio using Visual Studio and CLI and publish it as a package. We shall see overall how to create the NuGet package and publish it to the Nuget.org repository and later consume it from NuGet. You can publish it to your custom enterprise registry similar to nuget.org if any as well.

In today’s article, we shall cover the below steps for Create and Publish NuGet Package,

  • Prerequisites– Visual Studio or Nuget CLI
  • Create a .NET Core or .NET Standards class library project
  • Configure and Create Nuget Package properties
  • Create Nuget Package(“.nupkg”)
  • Acquire NuGetAPI Key
  • Publish NuGet Package from Visual Studio
  • Publish NuGet Package from Build
  • Install and Test the package

What is Nuget

The NuGet package is a ZIP file with .nupkg extension that contains resources in the form code (DLLs), files, metadata manifest, etc. This file can be put into to host like Example: nuget.org or any of your custom repositories so that the package and the code become easily shareable and reusable as required.

create nuget package visual studio 2019

Getting started

Prerequisites

  1. Install any edition of Visual Studio 2019 or .NET CLI
  2. Create an account in nuget.org

Create a .NET Core or .NET standards class library project

  1. Create a .NET Standards/or .NET Core project

I have below simple method defined in the Class Excelerator

 public class Excelerator : IExcelerator
    {
        public string GetData()
        {
            return "TheCodeBuzz";
        }
    }

Create Nuget Package Visual Studio 2019 Configuration

Right-click the project in Solution Explorer–> Properties–>Package tab.

generate nuget package on build

Kindly fill in metadata details like Package ID, Version, Authors details, Company Name, Licensing, etc.

Create a Nupkg File and package

Right-click the project in Solution Explorer and select the Pack

nuget create package

OR

Optionally Generate package on build

create nuget package visual studio

The above option shall create a .nupkg package file as shown below,

\ExcelSoft.NET\bin\Debug\ExcelSoft.NET.1.0.0.nupkg

Acquire your API key

Please sign in to your NuGet.org account and acquire a unique Key.

publish nuget package

Note: Make sure to copy your new API key now using the Copy button below as it is a one-time-key.

Publish Nuget package

Please open CLI commands within the same project directory where your .nupkg exists.

Publish Command:

dotnet nuget push ExcelSoft.NET.1.0.0.nupkg -k oy2huxfnvq3p7pat6fya3zo7lmfvdgvupmvt6nfcacdezm -s https://api.nuget.org/v3/index.json

After successful package push, you shall success message as below,

dotnet nuget push ExcelSoft.NET.1.0.0.nupkg -k oy2huxfnvq3p7pat6fya3zo7lmfvdgvupmvt6nfcacdezm -s https://api.nuget.org/v3/index.json
info : Pushing ExcelSoft.NET.1.0.0.nupkg to 'https://www.nuget.org/api/v2/package'…
info : PUT https://www.nuget.org/api/v2/package/
warn : All published packages should have license information specified. Learn more: https://aka.ms/deprecateLicenseUrl.
info : Created https://www.nuget.org/api/v2/package/ 1726ms
info : Your package was pushed.

View the published package

how to create a nuget package in visual studio

Let’s search for the published package in the Nuget repository and then install the package,

how to create nuget package in visual studio 2019

Finally, we are able to invoke library API easily,

visual studio create nuget package

Reference :

That’s All! Enjoy Coding !!

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *