Creating, Publishing, and Using a .NET Core NuGet Package

Introduction

This step-by-step guide will walk you through the process of creating, publishing, and using a .NET Core NuGet package. NuGet packages are essential in .NET development, enabling you to share and distribute libraries and code efficiently. Whether you're a library author looking to share your code or a developer seeking to simplify project dependencies, this guide will provide you with the knowledge and skills to navigate the world of .NET Core NuGet packages effectively. Let's get started!

What is a NuGet Package?

 A NuGet package is a standardized way to share and distribute .NET libraries and code.

Why Use NuGet Packages?

NuGet packages promote code reusability, modularity, version management, and easier collaboration in .NET projects.

Key Concepts

  • .NET Core Library Project: Where you create code to package.
  • NuGet Package Metadata: Includes ID, version, authors, description, and dependencies.
  • NuGet.org: The central repository for NuGet packages.
  • NuGet CLI: The command-line tool for managing packages.

Create .NET core Library Project

Create .NET core Library Project

To create a ClassLibrary named "FinanceMath" in C#, you can follow these steps:

Create .NET core Library Project

Create .NET core Library Project

To create a class named "InterestCalculation" within your "FinanceMath" Class Library and implement a function for Simple Interest (SI) calculation, you can follow these steps in C#:

Create .NET core Library Project

Go to the Class library file, right click, select the properties section, and open the setting window here. So add the package version here and Save.

Create .NET core Library Project

Create .NET core Library Project

  1. In your "FinanceMath" Class Library project, right-click on the project in the Solution Explorer.

  2. Select "Properties."

  3. In the project properties, go to the "Package" tab. This tab contains options related to creating a NuGet package.

  4. Configure the package metadata, such as the package ID, version, authors, description, etc. This information will be used to identify and describe your package.

  5. Build your project to ensure that it compiles successfully.

  6. Once your project is built, you can create the NuGet package using a tool such as the NuGet CLI, Visual Studio's built-in package creation tools, or a NuGet package manager GUI like NuGet Package Explorer

  7. After creating the package, you can publish it to a NuGet feed or distribute it as needed.

Create .NET core Library Project

To Build ClassLibrary file, build  Package

Create .NET core Library Project

F:\MachineTest\FinanceMath\FinanceMath\bin\Debug

Create .NET core Library Project

Publishing Your NuGet Package

Once you have a nuget.org account and are signed in, you can utilize the nuget.org web portal to publish your package. This process typically involves uploading your .nupkg file and providing essential package metadata, including the package ID, version, authors, description, and other pertinent information.

Publishing NuGet Package

To upload a package to the nuget.org website, click on 'Upload' in the top menu, navigate to the package on your computer, and then select 'Open'.

Publishing NuGet Package

Publishing NuGet Package

After you have prepared all the necessary information, click on 'Submit.' Once you've submitted the NuGet package and received a confirmation email from Gmail regarding the registration of your Microsoft account, the entire process will be complete, and your NuGet package will be published globally for use.

Using the Published Nuget Package

Once your package is published, it becomes accessible for use by other developers in the .NET community. 

Using the Published Nuget Package

After the package is published, you have the option to install and use it in your API or any other project

Using a .NET Core Custom NuGet Package

Create a .NET Core API application and use a NuGet package.

Install FinanceMath through NuGet Package Manager

The Visual Studio software offers the NuGet Package Manager option, allowing you to install the package directly into the solution.

In Visual Studio, navigate to Tools > NuGet Package Manager > Manage NuGet Packages for the solution. The screenshot below illustrates how to open the NuGet Package Manager.

Install FinanceMath through NuGet Package Manager

  • [HttpPost("SI")]: This is an attribute that specifies that this action should respond to HTTP POST requests with the URL route "SI."
  • public async Task<ActionResult> GetSICalculation([FromQuery] int Amount, int Rate, int Year): This is the action method. It's asynchronous (async) and expects three integer parameters (Amount, Rate, and Year) to be passed in the query string.

Calculate the Simple Interest by invoking the SI method from the InterestCalculation class within the FinanceMath package. This method should be integrated into the API's functionality on the server side

After testing this API on the user side with parameters: Check Amount = 1000, Rate = 8, and Years = 5, the function was executed, and the final result was obtained, which is Simple Interest = 1400.

Conclusion

Creating, publishing, and using a .NET Core NuGet package is a powerful way to share code, libraries, and components efficiently in your development projects and with the broader community. This article has walked you through the key steps involved in this process