Getting started with .NET Nuget Package Manager

Getting started with .NET Nuget Package Manager

Publish to Nuget Source and Private Github Packages


3 min read

Package managers main purpose is to distribute your code as a library for other developers to use. Few well-known library managers npm for node, similarly .NET has nuget

There tons of memes on size of node_modules folder ๐Ÿ˜›

At the end of the article we'll be able to create your own NuGet Package and use it across your applications

Prerequisite knowledge

1) Create .NET Application

Create a new project

dotnet new classlib -n IsOdd

Let's create a custom IsOdd method

namespace IsOdd;
public class Utils
    public static bool IsOdd(int value) => (value & 1) == 0;

    public static bool IsOdd(long value) => (value & 1) == 0;

2) Setting up Nuget Configuration

To create a nuget package we need to add few configuration in <ProjectName>.csproj

  <PackageId>NugetDemo</PackageId><!-- Unique package Identifier -->
  <Authors>dchennaraidu</Authors><!-- Author of Nuget -->
  <Company>dchennaraidu</Company><!-- Optional -->
  <IncludeSymbols>true</IncludeSymbols><!-- To view source in referenced project -->
  <SymbolPackageFormat>snupkg</SymbolPackageFormat><!-- Earlier it was .symbols.nupkg, now only allos snupkg -->
  <PackageDescription>NugetDemo</PackageDescription><!-- Optional, good to have -->
  <RepositoryUrl></RepositoryUrl><!-- Source code repository-->

3) Setting Config Tokens

Using Github

Visit Github, and grab a token to publish the NuGet package. Required permissions

  • write:packages
  • read:discussion


Configure Github Source with Token

dotnet nuget add source \
  --store-password-in-clear-text --name "github" \
  "<username_org_name>/index.json" \
  --username <github_username> --password "<your_token>"

Using Public Nuget

Visit Nuget API Keys, copy the token Screenshot 2022-05-08 at 23.38.35.png

This configuration is stored in local user configuration

  • Windows: %appdata%\NuGet\config\*.Config
  • Mac/Linux: ~/.config/NuGet/config/*.config or ~/.nuget/config/*.config

In case you have misconfigured run dotnet nuget remove source "github" and re-add

4) Package and Publish the library

Now let's package the class library

dotnet pack IsOdd.csproj -c Debug \
  -o artifacts \
-cconfiguration - default is Debug, you might want to use Release
/p:VersionEach package will need to have incremental versioning, so here we have use calendarVer
-oOutput path

Private Github Source

dotnet nuget push "artifacts/*" --source github \
  --skip-duplicate -k "<github_token>"

๐Ÿšจ Github takes a few minutes to reflect the package for the first time, later consecutive versions are shown quickly

Nuget Source

๐Ÿšจ Once a NuGet package is published to an official source, you cannot delete it, the only way it unlisting it

dotnet nuget push "artifacts/*" \
  -s \
  -k "<nuget_token>"<org/username>?tab=packages&repo_name=<github_reponame>

5) Utilising the Package

Create another separate project

dotnet new mvc -n MyApp1 --no-https

Create nuget.config file, when dotnet tries to restore packages it will look for the relevant package at explicit sources only, although this is optional, if not configured it will look for package in all the sources, we can save our crucial seconds ๐Ÿ˜.

<?xml version="1.0" encoding="utf-8"?>
    <clear />
    <add key="nuget" value="" />
    <add key="github" value="<username_orgname>/index.json" />

    <packageSource key="github">
      <package pattern="Chan.*" /> <!-- Your package pattern -->
    <packageSource key="nuget">
      <package pattern="*" />

Add package

dotnet add package IsOdd

Things to Note

Even tough we are including symbols(source code) to debug, we cannot debug if we are using VS Code editor, it's suggested to use Visual Studio 2022 or JetBrains Rider

Github Limits image.png

Please consider dropping a like ๐Ÿ‘๐Ÿป or reacting to the blog, thank you๐Ÿ™๐Ÿผ


Did you find this article valuable?

Support Chenna by becoming a sponsor. Any amount is appreciated!