C#[Essential Skills] Make NuGet package and publish it to NuGet official website

C#[Essential Skills] Make NuGet package and publish it to NuGet official website

Very detailed tutorial

最后更新 10/10/2023 1:28 PM
明如正午
预计阅读 4 分钟
分类
.NET
标签
.NET C# NuGet

1. Preparation: Create and obtain API Keys on NuGet

Website: www.nuget.org/

1.1. You need to log in first, just log in directly with your Microsoft account

1.2. Click the menu API Keys in the upper right corner to create a Key

1.3. Fill in the information and create

Write here the name of the NuGet package you want to upload [Each NuGet package (corresponding to one name) can upload multiple versions]

1.4. Copy API Key

2. Make 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;
        }
    }
}

框架使用.NET Framework 4.6.1,这个在后面也会使用到,输出类型使用类库:

Generate the following dll:

3. Create a publishing folder

为了更好管理文件,在E:\nuget新建MyPackage_Star302Test文件夹,存放所需的文件

4. Upload and publish NuGet package

4.1. 方法一:使用命令行上传,需要下载nuget.exe

4.1.1. 到https://www.nuget.org/downloads下载nuget.exe

4.1.2. Configure nuget environment variables

  • 把下载的 nuget.exe 放到E:\nuget

  • 打开电脑属性–高级系统设置–环境变量–系统变量,选择 Path–编辑–新建–填写E:\nuget,确定

Once the environment variables are configured, you can use the nuget instruction

4.1.3. Generating nuspec file

Use the nuget spec command to produce.nuspec files

.nuspec 文件中的内容为xml格式,如下,可以简单了解:

The changed 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. Generating nupkg file

使用nuget pack命令生产.nupkg 文件

4.1.5. Copy the API Key of 1.4 and upload it 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 failed, I don't know why]

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 files

4.2.4. Edit uploaded data

这里选择Edit Metadata,Edit Metadata Source 则是 xml 文件格式的

Change some parameters:

Add project dependencies, that is, which frameworks you rely on. Such as. NET Framework,. NET Standard, etc.

Finally, click Confirm

Editing completed, check the effect

Looking at the content corresponding to the Metadata Source, it is actually consistent with the information just edited.

4.2.5. Save nupkg file

4.2.6. Published to NuGet

Successfully released:

Check in Visual Stuido:

5. Management of NuGet packages

Just go to https://www.nuget.org/and explore it yourself. It's very simple.

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

Copyright statement: This article is an original article by CSDN blogger "Ming Like Noon" and follows the CC 4.0 BY-SA copyright agreement. Please attach a link to the original source and this statement to reprint it.

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

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 5/24/2025

Hello. NET run file, bye 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 project files. This feature makes it easy to develop scripts and simple applications by generating virtual project files in memory and supporting NuGet dependency packages and project property settings. The article also looks forward to the future of this feature, including target path extensions, unified command line parameters, performance improvements, and more file-based program command support.

继续阅读
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读