Skip to content

NuGet 全套教程指南,涵盖从基础到进阶的所有核心操作,包括 使用、发布、管理私有包 等场景。教程分为 6个阶段 ,并提供具体命令和示例。


阶段 1:NuGet 基础入门

1. 安装与配置

  • 安装 NuGet 客户端
    • 已内置在 Visual Studio 和 .NET SDK 中。
    • 单独安装(如 CLI 工具):
      bash
      dotnet tool install --global NuGet.CommandLine
  • 检查版本
    bash
    nuget help  # 或 dotnet nuget --version

2. 查找和安装包

  • 搜索公共包
    • 访问 nuget.org 或直接在 Visual Studio 的“NuGet 包管理器”中搜索。
  • 安装包到项目
    • 命令行(适用于 .NET Core+):
      bash
      dotnet add package Newtonsoft.Json --version 13.0.1
    • Visual Studio
      • 右键项目 → “管理 NuGet 包” → 搜索并安装。

3. 包依赖管理

  • 查看项目已安装的包:
    bash
    dotnet list package
  • 更新包:
    bash
    dotnet update package Newtonsoft.Json
  • 卸载包:
    bash
    dotnet remove package Newtonsoft.Json

阶段 2:项目配置与高级操作

1. 配置文件解析

  • .NET Core+/SDK 风格项目.csproj):
    xml
    <ItemGroup>
      <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    </ItemGroup>
  • 旧版 .NET Frameworkpackages.config):
    xml
    <package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />

2. 版本控制语法

  • 精确版本:13.0.1
  • 范围版本:
    • [13.0.1, 14.0.0)(≥13.0.1且<14.0.0)
    • *(最新版,不推荐)

3. 还原与缓存

  • 还原依赖(下载所有包):
    bash
    dotnet restore
  • 清除本地缓存:
    bash
    dotnet nuget locals all --clear

阶段 3:创建和发布自己的 NuGet 包

1. 创建类库项目

bash
dotnet new classlib -n MyAwesomeLibrary
cd MyAwesomeLibrary

2. 配置包元数据

编辑 .csproj 文件,添加包信息:

xml

<PropertyGroup>
    <PackageId>MyAwesomeLibrary</PackageId>
    <Version>1.0.0</Version>
    <Authors>YourName</Authors>
    <Description>A cool library for XYZ.</Description>
</PropertyGroup>

3. 生成 NuGet 包

bash
dotnet pack --configuration Release

生成的 .nupkg 文件位于 bin/Release/ 目录。

4. 发布到 NuGet.org

  1. 注册 NuGet.org 账号
  2. 获取 API Key(个人中心 → API Keys)。
  3. 推送包:
    bash
    dotnet nuget push bin/Release/MyAwesomeLibrary.1.0.0.nupkg --api-key YOUR_API_KEY --source https://api.nuget.org/v3/index.json

阶段 4:私有包源与公司内部使用

1. 搭建私有源

  • 选项 1:使用 Azure Artifacts(Azure DevOps)。
  • 选项 2:本地文件共享或 NuGet.Server。
  • 示例(本地文件夹源)
    bash
    # 添加本地源
    dotnet nuget add source --name MyLocalSource --address C:\MyPackages

2. 发布到私有源

bash
dotnet nuget push MyAwesomeLibrary.1.0.0.nupkg --source MyLocalSource

3. 从私有源安装

bash
dotnet add package MyAwesomeLibrary --source MyLocalSource

阶段 5:进阶技巧与问题排查

1. 包签名(安全验证)

xml

<PropertyGroup>
    <SignPackage>true</SignPackage>
    <CertificatePath>mycert.pfx</CertificatePath>
</PropertyGroup>

2. 多目标框架(Multi-Targeting)

支持同时为多个框架(如 .NET 6.0 + netstandard2.0)生成包:

xml

<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>

3. 调试 NuGet 包

  • 在包项目中启用调试符号:
    xml
    <PropertyGroup>
      <IncludeSymbols>true</IncludeSymbols>
      <SymbolPackageFormat>snupkg</SymbolPackageFormat>
    </PropertyGroup>

4. 常见问题

  • 错误:包冲突
    使用 PackageReference 替代 packages.config(现代项目推荐)。
  • 错误:版本不兼容
    检查依赖包的 TargetFramework

阶段 6:自动化与 CI/CD 集成

1. 在 GitHub Actions 中自动发布

yaml
- name: Publish to NuGet
  run: dotnet nuget push **/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json

2. 版本自动递增

使用 Nerdbank.GitVersioning 工具根据 Git 提交自动生成版本号。


资源推荐

  • 官方文档NuGet Documentation
  • 工具
    • NuGet Package Explorer(可视化编辑包)
    • BaGet(轻量级私有 NuGet 服务器)

✨ 网站运行时间: 3年11月15天 ❤️ 道阻且长,行则将至 - 微信号: heikedreamer