Post

C# | Create Nuget Package using .NET Standard

Tools and technologies used

  • Visual Studio 2022
  • .NET Standard 2.1
  • Nuget.exe

Implementation

New Project Creation

Under Visual Studio create a new Project Class Library and use .NET Standard 2.1 as target framework due to compatibility reason with latests versions of .NET CORE Frameworks.

Use Nuget CLI to generate files

1.Download Nuget.exe file Use the following link to download the latests version of Nuget.exe file.

2.Generate nuspec file Under the new project folder created befor, open a cmd console and run the comand bellow in order to generate the nuspec file.

1
nuget spec NewProjectName.csproj

The result of the command should generate a new file which has the content below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <license type="expression">MIT</license>
    <!-- <icon>icon.png</icon> -->
    <projectUrl>http://project_url_here_or_delete_this_line/</projectUrl>
    <description>$description$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>$copyright$</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
</package>

3.Generate nupkg file

You have two solutions order to generate the nuget package file(nupkg)

using the post-build event of the project

Under Visual Studio right click on the NewProjectName.crproj and select post-build event tab. After that put the command bellow and Build the solution

1
nuget pack "$(ProjectPath)" -Symbols -Properties Configuration=$(ConfigurationName) -IncludeReferencedProjects -OutputDirectory "C:\Dev\nuget_packages\NewProjectName\"

using the Nuget CLI command

Under the cmd window tape the command bellow in order to generate the nuget package

1
nuget pack MyProject.csproj -properties Configuration=Release -OutputDirectory "C:\Dev\nuget_packages\NewProjectName\"

In all cases the new nuget package file will be generated under the output directory : *C:\Dev\nuget_packages\NewProjectName*

What next ?

Once you’ve created a package, which is a .nupkg file, you can publish it to the gallery of your choice(Artifactory, Azure artifacts or GitHub Package registry)

This post is licensed under CC BY 4.0 by the author.