How to create and deploy a “Nuget Package” using Visual Studio
Tags: dotnet, nuget, tutorial, csharp
Published: 2023-05-25 00:52:56

# Resources

- Visual Studio 2017 or later.

# Tasks

- Creating an account in “[nuget.org](http://nuget.org/)”.
- Creating “Class Library” project and implement utility functionality.
- Configure package properties.
- Get API Key from “[nuget.org](http://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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yduppkjeeveg6i5o3f0.png)



### Steps

Go to [https://www.nuget.org/](https://www.nuget.org/)

Select the option “Sign in”.

Enter your Microsoft account or Azure Active Directory.

Accept all permissions gives to [Nuget.org](http://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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45ejsenbv1ifznkihhty.png)



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:

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

Result:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sq2t9ut7yal1mpi1qwn.png)



## Configure package properties

---

### Steps

In the properties of the project.

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o0ckqfc9l98pa18a44dg.png)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bkkfln6pkibv3cl9l7d.png)



## Get API Key from “nuget.org”

---

### Steps

Go to API Keys page in “nuget.org”


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvsapr9qxebocjv5vcbs.png)



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

Click in create button.

Result:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsgtq1xms4xbq7npt156.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3w20c9lqqlh1ybis8pc.png)



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
```

Result:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dj5vun2zejk9zspfwaxl.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84kcwtd6h3hivgv9mzgu.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8agp2qnu74kquz12i32j.png)



## 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
```

***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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ex7t3nyt3ygy60bb4nsi.png)



### Package Manager Console

Command:

```
NuGet\Install-Package CorporationCapsule.Util.Connectivity -Version 1.0.0
```

***Installing by Package Manager Console***

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

Run the command for “Package Manager Console”


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aiypzf0abfclq64t6605.png)



### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zf7in5dkya1wc03te2s.png)



Select the package and click in install.

Accept the change in the solution.


### Checking the installation

Before to add the package:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64shu97t5kkh82g2y02h.png)



After to add the package:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ablw0hrcsl1emoo3hs94.png)



Thank you for reading this .NET Series.

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