Getting started with .NET Nuget Package Manager

Getting started with .NET Nuget Package Manager

Publish to Nuget Source and Private Github Packages

Chenna's photo
Chenna
ยทMay 7, 2022ยท

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

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

<PropertyGroup>
  ...
  <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 nuget.org only allos snupkg -->
  <PackageDescription>NugetDemo</PackageDescription><!-- Optional, good to have -->
  <RepositoryUrl>https://github.com/dchennaraidu/nugetdemo</RepositoryUrl><!-- Source code repository-->
</PropertyGroup>

3) Setting Config Tokens

Using Github

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

  • write:packages
  • read:discussion

image.png

Configure Github Source with Token

dotnet nuget add source \
  --store-password-in-clear-text --name "github" \
  "https://nuget.pkg.github.com/<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 \
  /p:Version="YY.mm.dd.HHmm"
ParameterDescription
-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 https://api.nuget.org/v3/index.json \
  -k "<nuget_token>"

https://github.com/<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"?>
<configuration>
  <packageSources>
    <clear />
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="github" value="https://nuget.pkg.github.com/<username_orgname>/index.json" />
  </packageSources>

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

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๐Ÿ™๐Ÿผ

giphy

Did you find this article valuable?

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

See recent sponsors |ย Learn more about Hashnode Sponsors