Creating and Publishing .NET NuGet Packages

KUMAR BISHOJITKUMAR BISHOJIT
2 min read

SETP-1: Prerequisites

Verify installation: After a successful installation, you could verify by the cmd/bash.

dotnet --version

SETP-2: Create a class library project

Using this command, a project named MyAwesomePackage will be created.

dotnet new classlib -n MyAwesomePackage
cd MyAwesomePackage

SETP-3: Open an Editor and Add your code

XML comments are necessary for proper documentation.

namespace MyAwesomePackage;

/// <summary>
/// Provides basic arithmetic operations
/// </summary>
public class Calculator
{
    /// <summary>
    /// Adds two integers
    /// </summary>
    /// <param name="a">First number</param>
    /// <param name="b">Second number</param>
    /// <returns>Sum of a and b</returns>
    public int Add(int a, int b) => a + b;

    // Add similar documentation for Multiply
}

SETP-4: Configure Package Metadata

Here is a basic project setup file, MyAwesomePackage.csproj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net9.0</TargetFramework>

        <!-- Core Package Metadata -->
        <PackageId>MyAwesomePackage</PackageId>
        <Version>1.0.1</Version>
        <Authors>bishojit</Authors>
        <Company>BIKIRAN</Company>
        <Description>Utility functions for common operations</Description>
        <PackageTags>utilities helper tools</PackageTags>

        <!-- Recommended Additions -->
        <PackageProjectUrl>https://github.com/bikirandev/bikiran.utils</PackageProjectUrl>
        <RepositoryUrl>https://github.com/bikirandev/bikiran.utils.git</RepositoryUrl>
        <PackageLicenseExpression>MIT</PackageLicenseExpression>
        <IncludeSymbols>true</IncludeSymbols>
        <SymbolPackageFormat>snupkg</SymbolPackageFormat>

        <!-- Add below existing PackageTags -->
        <PackageReadmeFile>README.md</PackageReadmeFile>
    </PropertyGroup>

    <ItemGroup>
        <!-- Add NuGet.Protocol package reference -->
        <PackageReference Include="NuGet.Protocol" Version="6.13.2" />
    </ItemGroup>

    <ItemGroup>
        <None Include="README.md" Pack="true" PackagePath="\" />
    </ItemGroup>
</Project>

STEP-5: Build the Package

After developing all the operations, you need to build the package before publishing it.

dotnet build

STEP-6: Create NuGet Package

Make sure to fix all errors and warnings before publishing.

dotnet pack --configuration Release

STEP-7: Requirements before publishing

  1. Add a LICENSE File (Required)

  2. Add README.md (Required)

  3. I repeat, must add XML comment documentation on each class.

  4. Add Package Icon (Optional)

STEP-8: Publish to NuGet.org

  • Log in > Click Upload (On Top Navigation Bar)

  • Locate your file and upload it.

  • After a successful upload, complete the form.

  • After a few minutes, the Package will be approved.

Important Notes:

  1. Versioning: Increment <Version> before each new release

  2. The package ID must be unique across NuGet.org

  3. Recommended: Use Semantic Versioning

  4. For private feeds: Change --source to your feed URL

Next Steps:

  1. Add documentation in README.md

  2. Consider adding:

    • XML documentation comments

    • License file

    • Icon

    • Release notes

CONTACT:

I’m Kumar Bishojit Paul, the Founder and CEO of BIKIRAN. If you need further assistance, please leave a comment. I’m interested in helping you.

0
Subscribe to my newsletter

Read articles from KUMAR BISHOJIT directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

KUMAR BISHOJIT
KUMAR BISHOJIT