DEV Community

Cover image for How to create and deploy a “Nuget Package” using Visual Studio
Alejandro Junqui
Alejandro Junqui

Posted on

How to create and deploy a “Nuget Package” using Visual Studio

Resources

  • Visual Studio 2017 or later.

Tasks

  • Creating an account in “nuget.org”.
  • Creating “Class Library” project and implement utility functionality.
  • Configure package properties.
  • Get API Key from “nuget.org”.
  • Publish the package.
  • Installation “NuGet Package” in a client application.

Creating an account in “nuget.org”


Requirements

  • Active Microsoft Account

Note: You are able to select other ways to login.

Image description

Steps

Go to https://www.nuget.org/

Select the option “Sign in”.

Enter your Microsoft account or Azure Active Directory.

Accept all permissions gives to Nuget.org

Register your username.

  • Note: The username can’t be change or renamed, is case sensitive.

Click in register button.

Creating “Class Library” project and implement utility functionality


Steps

Open the Visual Studio

Select “Class Library” Project

Image description

Set the project name, the location and the solution name.

Set the framework “.NET 6.0 LTS”

Create the class “Connectivity” and copy and paste the next code:

public static bool CheckInternetConnectivity(string url)
{
    try
    {
        using (var client = new WebClient())
        using (var stream = client.OpenRead(url))
        {
            return true;
        }
    }
    catch (WebException)
    {
        return false;
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Image description

Configure package properties


Steps

In the properties of the project.

Activate the Check to “Generate Nuget package on build”.

Image description

Fill the next fields as generic information of your Nuget package: Title, Description, Authors, Company, Tags and etc.

(Optional) Change the project from “Debug” to “Release”

  • In both cases works fine.

Build the project

Result:

Image description

Get API Key from “nuget.org”


Steps

Go to API Keys page in “nuget.org”

Image description

Set name to API Key and set * in global patterns.

Click in create button.

Result:

Image description

Publish the package


To publish the package we have 2 options: Via GUI using “nuget.org” or Via .NET CLI.

Steps Via GUI

Go to “nuget.org” and select the tab “Upload”

Click in “Browse” control and select your file package.

Image description

Verify the package and click in submit.

Steps Via .NET CLI

Go to project and click in “Open folder in File Explorer”

Open a terminal and run the next command taking account the location of package and the api-key.

Command:

dotnet nuget push <path_nuget_package> --api-key <api_key> --source https://api.nuget.org/v3/index.json
Enter fullscreen mode Exit fullscreen mode

Result:

Image description

In “nuget.org”, select the “Packages” menu and in “Unlisted Packages” you are able to see the next:

Image description

After a few minutes of validation process the package is ready in “Published Packages”

Image description

Installation “NuGet Package” in a client application


To do this one we have 3 ways:

.NET CLI

Command:

dotnet add package CorporationCapsule.Util.Connectivity --version 1.0.0
Enter fullscreen mode Exit fullscreen mode

Installing by .NET CLI

Create or open any project in Visual Studio.

Select the project, right click and select the option “Open Folder in File Explorer”

Open a terminal in the path (cmd.exe)

Run the command for .NET CLI

Image description

Package Manager Console

Command:

NuGet\Install-Package CorporationCapsule.Util.Connectivity -Version 1.0.0
Enter fullscreen mode Exit fullscreen mode

Installing by Package Manager Console

In mene “Edit”, “Other Windows”, select “Package Manager Console”

Run the command for “Package Manager Console”

Image description

Manage Nuget Packages (GUI)

Browse the package “CorporationCapsule.Util.Connectivity”

Installing by Manage Nuget Packages (GUI)

Select the project, right click and and select the option “Manage Nuget Packages…”

In tab “Browse”, enter in the textbox “CorporationCapsule.Util”

Image description

Select the package and click in install.

Accept the change in the solution.

Checking the installation

Before to add the package:

Image description

After to add the package:

Image description

Thank you for reading this .NET Series.

Next Topic: How deploy an application in AWS Beanstalk using Visual Studio.

Top comments (0)