Introduction To NuGet Packages In C# - Tech Projects/Documentations
image

Introduction To NuGet Packages In C#

Introduction to NuGet packages in C#:

Case study: Windows applications built with C#

Author: Eze-Odikwa Tochukwu Jed

Introduction:

Almost every template in visual studio is made up of NuGet packages, if you have ever done any development in C# you most likely have used a NuGet package without even knowing it. NuGet allows us to create flexible applications that upgrade parts of itself without having to upgrade the entire codebase e.g. when a new version of dapper comes out I can upgrade to the new version, if there is a problem with the new version I can roll back to the old version.  

While every developer uses NuGet packages only very few create and host nuget packages. The large part of this issue comes from not knowing how to create nuget packages.

So what are NuGet packages?

A NuGet package is a single ZIP file with the .nupkg extension that contains compiled code (DLLs), other files related to that code, and a descriptive manifest that includes information like the package’s version number. Developers can publish their packages publicly for others to use, publish privately and have one compiled locally. Package consumers obtain packages from suitable hosts, add them to their projects, and then call a package’s functionality in their project code. NuGet itself then handles all of the intermediate details. A host serves as a point of connection between package creators and consumers. Once installed in a project, the packages’ APIs are available to the rest of the project code.

Because NuGet supports private hosts alongside the public nuget.org host, you can use NuGet packages to share code that’s exclusive to an organization or a work group. You can also use NuGet packages as a convenient way to factor your own code for use in nothing but your own projects. In short, a NuGet package is a shareable unit of code, but does not require nor imply any particular means of sharing.

illustration by Eze-Odikwa Tochukwu Jed

What you will learn  

  • Creating a sample NuGet package
  • Hosting a NuGet package

Creating a sample NuGet package: This is a step by step procedure for creating NuGet package in Microsoft Visual Studio 2022, launch the IDE and follow these procedures.

Step 1: select create new project and choose class Library (.NET Standard) click next and name your project in my case “TestingUnique9092”.

create a new project

Step 2: Delete the class that is created by default citing the code sample below

namespace TestingUnique9092
{
    public class Class1
    {
    }

Create a new folder within your project in my case “Calculations” then create a new class for the folder by right clicking and I will name my class calculator.cs. Now note we are not going into anything in-depth just creating a sample NuGet package.

Copy and paste this sample code into your calculator.cs file

namespace TestingUnique9092.Calculations
{
    public class Calculator
    {
        public static double Add(double x, double y) 
        { return x + y; }
        public static double Subtract(double x, double y) { return x - y; }

    }
}

If we want to share this and use in another project there are different ways we can go about it. We can:

  • Compile it and share it as a .DLL and import the .DLL into another project
  • put another project in this solution because the project is just a bucket so we can add a reference to create a relationship between the imported projects
  • Publish the project to a public or private repository, install and import it as a reference using NuGet tool in visual studio.

I will be covering how to publish it online and use in your project which leads us to.  

Step 3: Go to dev.azure.com on your browser and create a new project, if you don’t have an account you can easily sign up using github. After you create a new project you should head over to the artifacts section and create a new feed as shown below.

create a new feed

After creating a new feed click connect to feed then click NuGet, you should find sample commands for you to publish a package to this source yours might be different which of course depends on if things change.

azure artifacts

You might also want to download the suite of Nuget tools by checking for releases on github.com/microsoft/artifacts-credprovider.

Download the tools, copy and paste them into the bin folder of your project.

Step 4: Head back to visual studio, go to tools>options>package sources as shown below. Click add and fill the in the details which in my case is this below as shown in the pic.

NuGet Package Manager

Hosting a NuGet package: Right click on your .csproj file and select pack. This will compile your NuGet package as debug in the bin folder of your project with the .nupkg file extension. Locate that and copy out the directory path to a text editor.

Open windows PowerShell and cd to that directory by using the cd command in my case:

Cd C:\Users\spider\Desktop\PROJECTS\Project\TestingUnique9092\TestingUnique9092\bin\Debug

Initialize nuget.exe using this command:

&.\nuget.exe

Load the package using your own details of this command which in my case:

 &.\nuget.exe sources Add -Name "TestingUnique9092" -Source "https://pkgs.dev.azure.com/odikwatochy233/SampleLibrary/_packaging/TestingUnique9092/nuget/v3/index.json"

This should output some error ignore that and use this command to push your package

&.\nuget.exe push -Source "TestingUnique9092" -ApiKey AzureDevops TestingUnique9092.1.0.0.nupkg

Power Shell

For first timers this should pop a credentials manager dialog box prompting you to login to your Microsoft account after which your package should be published to your feed like in the image below:

Sample NuGet Package

Now you see publishing a NuGet package is that easy and simple, in subsequent topics we will showcase how to import and use a NuGet package in your project with lots of samples to practice with.

References:

github.com/microsoft/artifacts-credprovider

docs.microsoft.com/en-us/nuget/hosting-packages/overview

docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-sources

leave your comment


Your email address will not be published. Required fields are marked *

Uploading