Skip to content

Tag: dotnetcore

Creating a C# NuGet Package and Publishing it to NuGet.org

This turned out to be a lot simpler than I thought.

First, we start with a C# class library project, which we can create by running dotnet new classlib on the console. This will create a new project that has the name of the directory where the command runs. Many times, this is not what we want so we’ll either create a directory with the name of the project or use the --name flag to give our project a name (which creates this directory). In this case I’m going to use dotnet new project or --name MyFirstNuGet which creates a MyFirstNuGet directory containing two files: Class1.cs and MyFirstNuGet.csproj.

At this point, we are ready to create the package, by running dotnet pack. This creates a NuGet package that takes the name of the project for author and package identifier (a unique identifier for all packages, which is also used to reference the package using NuGet), and defaults to version 1.0.0.

We probably don’t want that, so let’s configure things a bit. NuGet properties are set in the csproj file inside a PropertyGroup (like any other MSBuild property). To me, the most important things we can set there are:

  • PackageId: the identifier of the package, which should be different from all other packages in NuGet.org, otherwise we will not be able to publish it there. And giving it a “catchy” name may also make it more popular, so choose wisely 😀.
  • Version: a sematic versioning compliant version for the package being created.
  • TargetFramework(s): a semicolon separated list of target frameworks that are supported by the package. This list can also have one element, and if you only have on element you can use TargetFramework.

Other than these two, we can add a list of Authors, License, Description, Copyright, and many others.

Let’s change these settings, run dotnet pack, and our package is ready!

As always, the (very small) code for this tutorial can be found in my GitHub repo.