Kanhaya Tyagi

NuGet Package -What you need to know

By Kanhaya Tyagi

Last updated cal_iconDecember 16, 2021

It’s never been easy to learn programming. But despite tons of ways to learn how to code, we believe that the best way to improve your skills is by contributing to open-source projects.

As per Microsoft, a crucial tool for any modern development platform is a mechanism for developers to create, share, and consume useful code. Often such code is bundled into “packages” that contain compiled code (as DLLs) and other content needed in the projects that consume these packages.

For .NET (including .NET Core), the Microsoft-supported mechanism for sharing code is NuGet, which defines how packages for .NET are created, hosted, and consumed, and provides the tools for each of those roles.

a). Life cycle of nuGet

Prerequisites

Step-1: Create an account in Nuget.org

Once you create an account, navigate to the profile and create API Keys(Global pattern is *). Copy the API key and place it in a secure place. An API key is a token that can identify you to NuGet Gallery.

API Key looks like “oy2cqgd6ch3o6votpmeq******ycze5aqsxutcq6d2am”.

Step-2: Create a Class Library (.NET Standard) name as CalculatorPOC with some calculation functions.

Create a class name as Calculator and paste the below code into it.

using System;
namespace CalculatorPOC
{
public static class Calculator
{
/// <summary>
/// The function is used to compute the Addition of two integer numbers
/// </summary>
/// <param name=”firstNumber”>Input type int</param>
/// <param name=”secondNumber”>Input type int</param>
/// <returns>Addition of two Integer values</returns>
public static int Addition(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
/// <summary>
/// The function is used to compute the Subtraction of two integer numbers
/// </summary>
/// <param name=”firstNumber”>Input type int</param>
/// <param name=”secondNumber”>Input type int</param>
/// <returns>Subtraction of two Integer values</returns>
public static int Subtraction(int firstNumber, int secondNumber)
{
return firstNumber – secondNumber;
}
/// <summary>
/// The function is used to compute the Multiplication of two integer numbers
/// </summary>
/// <param name=”firstNumber”>Input type int</param>
/// <param name=”secondNumber”>Input type int</param>
/// <returns>Multiplication of two Integer values</returns>
public static int Multiplication(int firstNumber, int secondNumber)
{
return firstNumber * secondNumber;
}
/// <summary>
/// The function is used to compute the Division of two integer numbers
/// </summary>
/// <param name=”firstNumber”>Input type int and as numerator</param>
/// <param name=”secondNumber”>Input type int and as denominator</param>
/// <returns>Division of two Integer values</returns>
public static int? Division(int firstNumber, int secondNumber)
{
if (secondNumber == 0)
{
return ;
}
return firstNumber / secondNumber;
}
}
}
//end of code

b). Example of calculator class.

Step-3:

a). Now right click on the CalculatorPOC project and navigate to properties.

Save the fields.

c). Again right-click on the CalculatorPOC project and navigate to the Edit CalculatorPOC.csproj

Once the changes are done, right-click on the project and click on the Pack to create NuGet packages.

Once the changes are done, right-click on the project and click on the pack to create NuGet packages.

Now, open the created package path in CMD which is looks as ‘C:\Users\kanhaya\Documents\Visual Studio 2017\Projects\CalculatorPOC\CalculatorPOC\bin\Debug\CalculatorPOC.1.0.0.nupkg’.

Copy and paste this command and replace YourPackage to your package name and APIkey with your APIkey from the NuGet into the CMD and press enter :

dotnet nuget push YourPackage.nupkg -k APIkey -s https://api.nuget.org/v3/index.json

All done the NuGet Package is uploaded to the NuGet server.

Step-4: For testing the NuGet, Create a console-application name as TestNuget and right-click on the

Project TestNuget and navigate to “Manage Nuget Packages….”

Search your package name. In my case, the name is “CalculatorPOC.” Click and install into the project.

NuGet used in the console-application.

Thanks for reading…

Get In Touch

How Can We Help ?

We make your product happen. Our dynamic, robust and scalable solutions help you drive value at the greatest speed in the market

We specialize in full-stack software & web app development with a key focus on JavaScript, Kubernetes and Microservices
Your path to drive 360° value starts from here
Enhance your market & geographic reach by partnering with NodeXperts