Logo

dev-resources.site

for different kinds of informations.

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

Published at
5/25/2023
Categories
dotnet
nuget
tutorial
csharp
Author
ajunquit
Categories
4 categories in total
dotnet
open
nuget
open
tutorial
open
csharp
open
Author
8 person written this
ajunquit
open
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.

nuget Article's
30 articles in total
Favicon
Simplifying Dependency Management with NuGet Central Package Management
Favicon
Failed to access Nuget in China
Favicon
Seu Primeiro Pacote NuGet: Um Passo a Passo Prático
Favicon
Central Package Management in .NET - Simplify NuGet Dependencies
Favicon
Custom NET8 Entity Framework Core Generic Repository
Favicon
How to publish a package on Nuget.org
Favicon
Automate Your C# Library Deployment: Publishing to NuGet and GitHub Packages with GitHub Actions
Favicon
My First NuGet Package: EmojiToText
Favicon
Publish C# Project to Nuget.org
Favicon
My Very First NuGet package
Favicon
Introducing VirtualStorageLibrary: A .NET Solution for In-Memory Tree Structures
Favicon
C# | Create Nuget Package using .NET Standard
Favicon
GitHub | Deploy .net core NuGet Packages in GitHub Packages Registry
Favicon
CSV Schema Validation
Favicon
Publishing Nuget in GitHub Packages
Favicon
.NET MAUI: Update NuGet Packages using Visual Studio Code
Favicon
How I Built a NuGet Package
Favicon
Lee's opinions on Umbraco + naming things
Favicon
Migrating the XM Cloud Introduction Repo to a new Nuget feed.
Favicon
How This NuGet Package Almost Cost Me My Job
Favicon
How to create and deploy a “Nuget Package” using Visual Studio
Favicon
How to change default Nuget packages folder on Windows
Favicon
How to Make a NuGet Package for C++ Development in Visual Studio
Favicon
Data Validation Nuget Package
Favicon
Create a NuGet-Package
Favicon
AspNetCore.VersionInfo 1.1.0 is out
Favicon
Deploying NuGet packages with Docker in GitHub actions
Favicon
Introducing TF WhatsUp, a Tool for Better Terraform Notes
Favicon
Creating a Nuget Package From a .Net 6 class library
Favicon
Elasticsearch.Net vs NEST

Featured ones: