NuGet 从入门到精进全解析
一、入门:NuGet 基础概念与简单操作
1. 什么是 NuGet?
- 定义:.NET 的包管理器,用于分发和引用第三方库(如 `Newtonsoft.Json`、`EntityFramework`)。
- 核心功能:
- 一键安装/更新/卸载依赖。
- 自动处理依赖关系(如 A 依赖 B,安装 A 时自动安装 B)。
- 版本控制(可指定精确版本或范围)。
2. NuGet 的基本组件
- 包源:
- 官方源:`https://api.nuget.org/v3/index.json`
- 私有源:企业内部服务器或 GitHub Packages。
- 包格式:`.nupkg` 文件(本质是 ZIP 压缩包,包含 DLL、配置文件等)。
- 配置文件:
- `packages.config`(旧版,已逐渐淘汰)。
- `PackageReference`(新版,直接写在 `.csproj` 中)。
二、安装与配置
1. 安装 NuGet 工具
#方法1:Visual Studio 内置(推荐)
#工具 → NuGet 包管理器 → 管理解决方案的 NuGet 包#方法2:命令行工具(nuget.exe)
choco install nuget.commandline
2. 配置包源
查看现有源
nuget sources list添加私有源
nuget sources add -Name "MySource" -Source "https://my-nuget-server/api/v3"禁用/启用源
nuget sources disable -Name "MicrosoftVisualStudioOfflinePackages"
三、基础操作
1. 安装包
方法1:Visual Studio 界面右键项目 → 管理 NuGet 包 → 浏览 → 搜索包名 → 安装方法2:Package Manager Console
Install-Package Newtonsoft.Json -Version 13.0.1方法3:.NET CLI
dotnet add package Newtonsoft.Json --version 13.0.1
2. 更新包
更新单个包
Update-Package Newtonsoft.Json -Version 13.0.2更新所有包
Update-Package安全更新(仅更新补丁版本)
Update-Package -Reinstall -Safe
3. 卸载包
Uninstall-Package Newtonsoft.Json
四、进阶:高级用法与最佳实践
1. 版本控制
安装特定版本
Install-Package Newtonsoft.Json -Version 13.0.1安装最新稳定版(不含预发布)
Install-Package Newtonsoft.Json -Version *安装最新预发布版
Install-Package Newtonsoft.Json -Version *-*版本约束(PackageReference 语法)
<PackageReference Include="Newtonsoft.Json" Version="[13.0.0, 14.0.0)" />[13.0.0, 14.0.0):包含 13.0.0,但不包含 14.0.0
2. 私有包源
添加认证的私有源
nuget sources add -Name "MyPrivateSource" -Source "https://my-server/v3/index.json" -UserName "user" -Password "pass"使用环境变量认证(更安全)
nuget sources add -Name "GitHub" -Source "https://nuget.pkg.github.com/org/index.json" -StorePasswordInClearText
然后在环境变量中设置 GITHUB_TOKEN
3. 创建自定义包
1. 创建 .nuspec 文件
nuget spec MyPackage2. 编辑 .nuspec 配置元数据
<?xml version="1.0"?>
<package ><metadata><id>MyPackage</id><version>1.0.0</version><authors>Me</authors><description>My awesome package</description></metadata><files><file src="bin\Release\MyPackage.dll" target="lib\net6.0" /></files>
</package>3. 打包
nuget pack MyPackage.nuspec4. 发布到源
nuget push MyPackage.1.0.0.nupkg -Source "MySource" -ApiKey "API_KEY"
五、性能优化与问题排查
1. 性能优化
1. 启用包缓存
nuget config -set globalPackagesFolder "C:\NuGetPackages"2. 预还原包(避免每次构建都下载)
dotnet restore3. 使用 PackageReference 替代 packages.config
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2. 常见问题排查
清理缓存
nuget locals all -clear验证包是否损坏
nuget verify -All MyPackage.1.0.0.nupkg查看详细日志
nuget install MyPackage -Verbosity detailed
六、实战:企业级应用
1. 包源镜像(加速下载)
使用国内镜像
nuget sources add -Name "NuGetChina" -Source "https://nuget.cnblogs.com/v3/index.json"
2. 自动化打包与发布(CI/CD)
GitHub Actions 示例
name: NuGet Packageon:push:tags:- 'v*' 匹配 v1.0, v2.0.3 等标签jobs:build:runs-on: windows-lateststeps:- uses: actions/checkout@v2- name: Setup .NETuses: actions/setup-dotnet@v1with:dotnet-version: 6.0.x- name: Restore dependenciesrun: dotnet restore- name: Buildrun: dotnet build --no-restore- name: Testrun: dotnet test --no-build- name: Packrun: dotnet pack --no-build -c Release -o nuget-packages- name: Publish to NuGetrun: |dotnet nuget push "nuget-packages/*.nupkg" --source "https://api.nuget.org/v3/index.json" --api-key ${{ secrets.NUGET_API_KEY }}--skip-duplicate
七、安全与合规
1. 依赖审计
检查依赖中的安全漏洞
dotnet list package --vulnerable使用第三方工具(如 WhiteSource Bolt)
2. 包签名
创建签名证书
nuget key create "MyKey" -OutputDirectory .签名包
nuget sign MyPackage.1.0.0.nupkg -CertificatePath "MyKey.pfx" -CertificatePassword "pass"验证签名
nuget verify -Signatures MyPackage.1.0.0.nupkg
八、总结与最佳实践
1. 推荐工具链
- - 开发:Visual Studio + NuGet 包管理器。
- - 命令行:`dotnet CLI` 或 `nuget.exe`。
- - 自动化:GitHub Actions / Azure Pipelines。
- - 私有源:NuGet.Server / GitHub Packages / Azure Artifacts。
2. 最佳实践
- - 锁定版本:避免使用 `*` 通配符,明确指定版本范围。
- - 定期更新:使用 `dotnet list package --outdated` 检查过期包。
- - 避免冗余:删除不需要的包,减少依赖膨胀。
- - 使用私有源:企业内部库优先发布到私有源。
- - 安全审计:定期扫描依赖中的漏洞。