C# [Essential Skills] Creating NuGet Packages and Publishing to NuGet Official Website

C# [Essential Skills] Creating NuGet Packages and Publishing to NuGet Official Website

Very detailed tutorial

Last updated 10/10/2023 1:28 PM
明如正午
4 min read
Category
.NET
Tags
.NET C# NuGet

1. Preparation: Create and Get API Keys on NuGet

URL: https://www.nuget.org/

1.1. First, log in directly with your Microsoft account

1.2. Click the API Keys menu in the top right corner, then create a Key

1.3. Fill in the information and create

Here enter the name of the NuGet package you want to upload 【Each NuGet package (corresponding to a name) can have multiple versions uploaded】

1.4. Copy the API Key

2. Create a simple dll

Create a new "Library" project with the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
        public static int Sub(int a, int b)
        {
            return a - b;
        }
    }
}

The framework uses .NET Framework 4.6.1, which will also be used later, and the output type is Class Library:

Generate the following dll:

3. Create a publish folder

To manage files better, create the folder E:\nuget\NewMyPackage_Star302Test to store required files

4. Upload the NuGet package and publish

4.1. Method 1: Upload via command line, requires downloading nuget.exe

4.1.1. Download nuget.exe from https://www.nuget.org/downloads

4.1.2. Configure nuget environment variable

  • Place the downloaded nuget.exe in E:\nuget

  • Open Computer Properties – Advanced System Settings – Environment Variables – System Variables, select Path – Edit – New – fill in E:\nuget, then OK

After configuring the environment variable, you can use the nuget command.

4.1.3. Generate the nuspec file

Use the nuget spec command to produce the .nuspec file

The content of the .nuspec file is in xml format as shown below; you can get a brief understanding:

The modified content is:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>PackageTest</id>
    <version>1.0.0</version>
    <authors>Star302</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <license type="expression">MIT</license>
    <!-- <icon>icon.png</icon> -->
    <projectUrl>http://project_url_here_or_delete_this_line/</projectUrl>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>$copyright$</copyright>
    <tags>Tag1 Tag2</tags>
    <dependencies>
      <group targetFramework=".NETStandard2.1">
        <dependency id="SampleDependency" version="1.0.0" />
      </group>
    </dependencies>
  </metadata>
</package>

4.1.4. Generate the nupkg file

Use the nuget pack command to produce the .nupkg file

4.1.5. Copy the API Key from 1.4 and upload to NuGet

Run the following command:

nuget push PackageTest.1.0.0.nupkg xxxkey -Source https://api.nuget.org/v3/index.json

【I tried several times but it didn't work for unknown reasons】

4.2.1. Download NuGet Package Explorer

https://www.microsoft.com/zh-cn/p/nuget-package-explorer/9wzdncrdmdm3?activetab=pivot:overviewtab

4.2.2. Create a new Package

4.2.3. Add lib folder, add net461 folder, add existing file

4.2.4. Edit upload data

Here select Edit Metadata; Edit Metadata Source is in XML file format

Change some parameters:

Add project dependencies, i.e., which frameworks to depend on, such as .NETFramework, .NETStandard, etc.

Finally, click Confirm

After editing, check the result

Look at the corresponding content of Metadata Source; it matches the information just edited.

4.2.5. Save the nupkg file

4.2.6. Publish to NuGet

Publish successful:

View in Visual Studio:

5. Managing NuGet packages

Explore it yourself at https://www.nuget.org/ – it's very simple.

Reference: https://blog.csdn.net/weixin_38211198/article/details/118438071

Copyright notice: This article is the original work of CSDN blogger "明如正午", licensed under CC 4.0 BY-SA. For reprinting, please attach the original source link and this statement.

Original link: https://blog.csdn.net/sinat_40003796/article/details/130407108

Keep Exploring

Related Reading

More Articles
Same category / Same tag 5/24/2025

Hello .NET run file, goodbye csproj

This article introduces the new file-based program feature of the .NET CLI, which allows developers to run C# source files directly without creating a project file. This feature works by generating a virtual project file in memory and supports NuGet dependencies and project property settings, providing convenience for developing scripts and simple applications. The article also looks forward to the future development directions of this feature, including target path extension, unified command-line arguments, performance improvements, and support for more file-based program commands.

Continue Reading