NashTech Blog

Develop a Custom NuGet Package in C#

Table of Contents
monitor, binary, binary system-1307227.jpg

When you work at an enterprise level, there will be lots of common code and solutions that repeat on several occasions. There are a multitude of ways to create it. By making common projects, utilities, libraries etc.

For C# project we can create these as NuGet packages.

This blog provides an idea to developers who want to create custom NuGet packages.

NuGet is the package manager for the Microsoft development platform, enabling developers to easily consume and distribute reusable components.

A NuGet package is a bundle of code, assets, and metadata contained in a single file with a .nupkg extension. It serves as the standard method for packaging and distributing libraries, tools, and other code artifacts within the .NET ecosystem.

Therefore, if we develop a reusable component, we can package it as a NuGet Package in .NET.

Features of NuGet Packages

  1. Packaging Format: NuGet packages use the .nupkg file extension.
  2. Versioning: NuGet packages adhere to Semantic Versioning (SemVer), where version numbers are formatted as MAJOR.MINOR.PATCH to indicate the compatibility and impact of updates.
  3. Metadata: Each package contains metadata that provides details such as the package’s name, version, author, description, dependencies, and other pertinent information in XML files named .nuspec.
  4. Target Frameworks: Packages can be built to target specific .NET frameworks or versions, allowing developers to use packages that are compatible with their project’s target framework.
  5. Dependencies: Packages specify dependencies on other NuGet packages, ensuring that all necessary dependencies are installed when the package is used.

Steps to Create a NuGet Package

  1. Create library project:
dotnet new classlib -o MyLibrary 
cd MyLibrary
  1. Update Assembly information:
  • Open the MyLibrary.csproj file in your favorite text editor (e.g., nano or vim).
  • Add the necessary assembly information within the <PropertyGroup> tags, such as:
    xml
<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
  <Authors>Your Name</Authors>
  <Company>Your Company</Company>
  <Description>Library Description</Description>
  <PackageVersion>1.0.0</PackageVersion>
</PropertyGroup>
  1. Package configuration in .nuspec file:
  • Create a .nuspec file, e.g., MyLibrary.nuspec, with the following content:
<?xml version="1.0"?>
<package >
  <metadata>
<id>MyLibrary</id>
<version>1.0.0</version>
<authors>Your Name</authors>
<owners>Your Company</owners>
<description>Library Description</description>
<dependencies>
   <!-- Add dependencies here -->
</dependencies>
  </metadata>
</package>
  1. Build the library:
dotnet build
  1. Create NuGet Package using dotnet CLI:
dotnet pack -p:NuspecFile=MyLibrary.nuspec
  1. Use the package locally without publishing:
  • Install the NuGet package locally:
dotnet add package MyLibrary --source ./bin/Debug
  • Restore the packages:
dotnet restore
  1. Publishing the library to public NuGet manager:
dotnet nuget push bin/Debug/MyLibrary.1.0.0.nupkg -k YourApiKey -s https://api.nuget.org/v3/index.json
  1. Publishing the library to custom NuGet locations:
  • Define a new source:
dotnet nuget add source --name CustomSource /path/to/your/nuget/source
  • Push the package to the custom source:
dotnet nuget push bin/Debug/MyLibrary.1.0.0.nupkg -s CustomSource

Benefits of NuGet packages

  • Code Reusability: Easily share and reuse code across multiple projects, reducing duplication and effort.
  • Simplified Dependency Management: Automatically handle dependencies, ensuring that all required libraries are available.
  • Version Control: Manage different versions of libraries, allowing projects to use specific versions and upgrade when necessary.
  • Consistency: Ensure consistency across projects by using standardized libraries, reducing bugs and discrepancies.
  • Distribution: Effortlessly distribute libraries within teams or publicly, facilitating collaboration and community contributions.
  • Isolation: Encapsulate functionality into discrete packages, promoting modular architecture and cleaner codebases.

Conclusion

Publishing a developed class library as a NuGet package for reusable code is a strategic architectural decision that promotes code reuse and standardisation. This approach enhances maintainability, reduces duplication, and streamlines updates across multiple projects, fostering a more efficient development workflow.

Picture of Vipul Kumar

Vipul Kumar

Leave a Comment

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

Suggested Article

Scroll to Top