How To Create and Publish NuGet Package Using Visual Studio With .NET 7

Introduction

NuGet is the most useful and essential tool for modern software development platforms via which developers can create, share and consume reusable code. Those useful or reusable common codes are compiled as DLLs and published as packages with code-related files and other information including the version number. As a developer, we are familiar with NuGet packages and use packages from the NuGet package manager. We all know that we can use any available NuGet package that is needed or suitable for our project from nuget.org. Anyone can build, publish and share the package through nuget.org. Furthermore, we can create a NuGet package using Microsoft Visual Studio and the .NET class library. Moreover, we can create open-source and non-open-source packages with license agreements.

In this article, we will create .NET 7 class library project and publish it as a NuGet package. Additionally, the article elucidates how we can create a NuGet package class library project, pack it using Visual Studio and upload it to nuget.org with an example and sample Logging code.

Prerequisites

  • Visual Studio 2022

Follow the below steps:

Create .NET 7 Class Library Project

Step 1

Open Visual Studio and click on Create a new project.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Step 2

Select a C# Class Library project template as depicted below.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Step 3

Then, give a project name and directory of the project as shown below and click on Next.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Step 4

Select Framework: .NET 7.0 as illustrated below and click on Create.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

After that, a default Class Library project will be created.

Step 5

Add a new Class Logger and write the below code.

Right-click on the project and click on Add option and then Class as shown below.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

For the demo purpose, we will write a simple logging code to generate the log. Simple code to write the log of class, method, and an error message is given below, just copy and paste it into your Logger.cs file. However, you can write your program based on your package purpose and requirement.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoPackage
{
    public class Logger
    {
        public void Log(string className, string methodName, string errorMessage)
        {
            Console.WriteLine("Class name:" + className);
            Console.WriteLine("Method name:" + methodName);
            Console.WriteLine("Error:" + errorMessage);
        }
    }
}

Now, our Logger program is ready, so we are ready to pack it. Before packing this project for NuGet we must configure the package property. Let’s configure the properties.

Configure package properties and create nupkg file

Step 1 - Configure package properties

Right-click on the project and go to project properties. Go to the Package tab and provide the Package ID, Title, Package version, Author Company, Product, Description, Tags, Repository URL, and so on. Additionally, you can make open-source and non-open-source packages which you can configure at this step. To make a better package, you can follow package best authoring practices.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Note: If you check the Produce a package file during build operations then Visual Studio will generate a NuGet package file, every time you build the project.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Once all necessary package properties are set then we can create a NuGet package.

Step 2 - Create/Publish NuGet Pack file

Once you provide all the package details and your project is ready to make for the package file then Right click on the project and then click on Pack as illustrated below.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Then it will create a nupkg file in your bin\Debug folder as portrayed in the below output screen. This nupkg file is the one that we need to upload to nuget.org.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Upload to NuGet

Step 1 - Create a free account on nuget.org.

Go to nuget.org and click on sign in. If you already have an account on nuget.org then you can use your existing account. However, If you don’t have an account on nuget.org then click on create an account and use a Microsoft account. Then, provide a username and click on the register as shown below.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Then you will sign into nuget.org.

Once signed in, you can update your profile if you want. NuGet will ask you to access your account details from your Microsoft account click on Yes as illustrated below.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

Step 2 - Upload the NUPKG file

Once you Login to nuget.org, go to the Upload tab as depicted below. Then browse and upload your nupkg file.

How to Create and Publish NuGet Package Using Visual Studio with .NET 7

It will check for viruses as well as take some time to validate and make the package to be available for use. Once published, you will get a confirmation email as well. Your published packages are publicly visible to other developers until you unlist them. However, you can publish your private packages available to only your organization or workgroup for this you can refer host your own NuGet feeds.

Conclusion

Hence, in this article, we learn to create a class library project in .NET 7 using Visual Studio 2022, configured the package properties, and created the NuGet package. Additionally, learn to create nuget.org account and uploaded the nupkg file. I hope this article has provided you an insight into creating and publishing a NuGet package using Visual Studio 2022 and .NET 7.

Reference