Packaging Libraries for NuGet Distribution
Explore the process of packaging .NET libraries for NuGet distribution. Understand how to add and fix package dependencies, build your library, and publish it to public or private NuGet feeds to share reusable code efficiently.
Before we learn how to create and package our own libraries, we will review how a project can use an existing package.
Referencing a NuGet package
Let’s say that we want to add a package created by a third-party developer, for example, Newtonsoft.Json, a popular package for working with the JavaScript Object Notation (JSON) serialization format:
Step 1: In the AssembliesAndNamespaces project, add a reference to the Newtonsoft.Json NuGet package, either using the GUI for Visual Studio 2022 or the dotnet add package command for Visual Studio Code.
Step 2: Open the AssembliesAndNamespaces.csproj file and note that a package reference has been added, as shown in the following markup:
Fixing dependencies
To consistently restore packages and write reliable code, it’s important that we fix dependencies. Fixing dependencies means we are using the same family of packages released for a specific version of .NET, for example, SQLite for .NET 7.0, as shown highlighted in the following markup:
To fix dependencies, every package should have a single version with no additional qualifiers. Additional qualifiers include betas ...