Community编程与开发github.com

dotnet-local-tools

通过 dotnet-tools.json 管理本地 .NET 工具,确保开发环境和 CI/CD 管道中使用一致的工具链。

dotnet-local-tools 是什么?

该技能专注于使用 .NET 本地工具机制,通过 `.config/dotnet-tools.json` 清单文件按仓库级别管理和版本化 CLI 工具(如 docfx、incrementalist、dotnet-ef 等),避免全局工具版本冲突。它支持在本地开发环境和 CI/CD 管道(包括 GitHub Actions 和 Azure Pipelines)中通过一条 `dotnet tool restore` 命令统一恢复所有工具。文档详细说明了工具清单的 JSON 格式、常用工具的安装与用法、版本更新策略以及最佳实践(如锁定版本、设置 isRoot 为 true、使用 Dependabot 自动更新等)。此外,还提供了排查工具未找到、版本冲突和清除缓存等常见问题的解决方案,以及一个完整的开发环境配置示例。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Aaronontheweb/dotnet-skills/tree/main/skills/local-tools

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

.NET 本地工具

何时使用此技能

在以下情况下使用此技能:

  • 在开发团队中建立一致的工具链
  • 确保 CI/CD 管道与本地开发使用相同版本的工具
  • 管理项目特定的 CLI 工具(如 docfx、incrementalist、dotnet-ef 等)
  • 避免项目间全局工具版本冲突

什么是本地工具?

本地工具是按仓库级别安装和版本化的 .NET CLI 工具,而非全局安装。它们通过 .config/dotnet-tools.json 定义,并通过 dotnet tool restore 恢复。

本地工具 vs 全局工具

方面全局工具本地工具
安装方式dotnet tool install -gdotnet tool restore
作用范围机器全局按仓库
版本控制手动在 .config/dotnet-tools.json 中
CI/CD每个工具单独安装单条恢复命令
冲突可能存在版本冲突项目间隔离

设置本地工具

初始化清单文件

# 创建 .config/dotnet-tools.json
dotnet new tool-manifest

这将创建:

.config/
└── dotnet-tools.json

本地安装工具

# 本地安装一个工具
dotnet tool install docfx

# 安装特定版本
dotnet tool install docfx --version 2.78.3

# 从指定源安装
dotnet tool install MyTool --add-source https://mycompany.pkgs.visualstudio.com/_packaging/feed/nuget/v3/index.json

恢复工具

# 从清单恢复所有工具
dotnet tool restore

dotnet-tools.json 格式

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "docfx": {
      "version": "2.78.3",
      "commands": [
        "docfx"
      ],
      "rollForward": false
    },
    "dotnet-ef": {
      "version": "9.0.0",
      "commands": [
        "dotnet-ef"
      ],
      "rollForward": false
    },
    "incrementalist.cmd": {
      "version": "1.2.0",
      "commands": [
        "incrementalist"
      ],
      "rollForward": false
    },
    "dotnet-reportgenerator-globaltool": {
      "version": "5.4.1",
      "commands": [
        "reportgenerator"
      ],
      "rollForward": false
    }
  }
}

字段说明

字段描述
version清单架构版本(始终为 1)
isRoot标记为根清单(阻止向上搜索父目录)
tools工具配置字典
tools.<name>.version要安装的精确版本
tools.<name>.commands工具提供的 CLI 命令
tools.<name>.rollForward是否允许更新版本(通常设为 false 以确保可重现性)

常用工具

文档生成

# DocFX - API 文档生成器
dotnet tool install docfx
"docfx": {
  "version": "2.78.3",
  "commands": ["docfx"],
  "rollForward": false
}

用法:

dotnet docfx docfx.json
dotnet docfx serve _site

Entity Framework Core

# EF Core 迁移 CLI
dotnet tool install dotnet-ef
"dotnet-ef": {
  "version": "9.0.0",
  "commands": ["dotnet-ef"],
  "rollForward": false
}

用法:

dotnet ef migrations add InitialCreate
dotnet ef database update

代码覆盖率

# ReportGenerator 覆盖率报告生成器
dotnet tool install dotnet-reportgenerator-globaltool
"dotnet-reportgenerator-globaltool": {
  "version": "5.4.1",
  "commands": ["reportgenerator"],
  "rollForward": false
}

用法:

dotnet reportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport -reporttypes:Html

增量构建

# Incrementalist - 仅构建更改的项目
dotnet tool install incrementalist.cmd
"incrementalist.cmd": {
  "version": "1.2.0",
  "commands": ["incrementalist"],
  "rollForward": false
}

用法:

# 获取自 main 分支以来受更改影响的项目
incrementalist --branch main

代码格式化

# CSharpier - 固定风格的 C# 格式化工具
dotnet tool install csharpier
"csharpier": {
  "version": "0.30.3",
  "commands": ["dotnet-csharpier"],
  "rollForward": false
}

用法:

dotnet csharpier .
dotnet csharpier --check .  # CI 模式 - 如果需要更改则失败

代码分析

# JB dotnet-inspect(需要许可证)
dotnet tool install jb
"jb": {
  "version": "2024.3.4",
  "commands": ["jb"],
  "rollForward": false
}

CI/CD 集成

GitHub Actions

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: 配置 .NET
        uses: actions/setup-dotnet@v4
        with:
          global-json-file: global.json

      - name: 恢复工具
        run: dotnet tool restore

      - name: 构建
        run: dotnet build

      - name: 测试并收集覆盖率
        run: dotnet test --collect:"XPlat Code Coverage"

      - name: 生成覆盖率报告
        run: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coveragereport

      - name: 构建文档
        run: dotnet docfx docs/docfx.json

Azure Pipelines

steps:
  - task: UseDotNet@2
    inputs:
      useGlobalJson: true

  - script: dotnet tool restore
    displayName: '恢复 .NET 工具'

  - script: dotnet build -c Release
    displayName: '构建'

  - script: dotnet test -c Release --collect:"XPlat Code Coverage"
    displayName: '测试'

  - script: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:$(Build.ArtifactStagingDirectory)/coverage
    displayName: '生成覆盖率报告'

管理工具版本

更新工具

# 更新到最新版本
dotnet tool update docfx

# 更新到特定版本
dotnet tool update docfx --version 2.79.0

列出已安装工具

# 列出本地工具
dotnet tool list

# 检查过时版本
dotnet tool list --outdated

卸载工具

dotnet tool uninstall docfx

最佳实践

1. 始终设置 isRoot: true

防止 MSBuild 向上搜索父目录的工具清单:

{
  "version": 1,
  "isRoot": true,
  ...
}

2. 锁定精确版本

使用 "rollForward": false 确保可重现构建:

"docfx": {
  "version": "2.78.3",
  "rollForward": false
}

3. 在 CI 中先恢复再使用

在使用任何本地工具之前,始终先运行 dotnet tool restore:

- run: dotnet tool restore
- run: dotnet docfx docs/docfx.json

4. 记录工具依赖

在 README 中添加注释或章节:

## 开发环境设置

1. 恢复工具:`dotnet tool restore`
2. 构建:`dotnet build`
3. 测试:`dotnet test`

5. 使用 Dependabot 管理更新

# .github/dependabot.yml
version: 2
updates:
  - package-ecosystem: "nuget"
    directory: "/"
    schedule:
      interval: "weekly"
    # 包含 .config/dotnet-tools.json 中的本地工具

故障排除

恢复后工具未找到

确保您从仓库根目录运行:

# 错误 - 从子目录运行
cd src/MyApp
dotnet docfx  # 错误:工具未找到

# 正确 - 从解决方案根目录运行
cd ../..
dotnet docfx docs/docfx.json

版本冲突

如果遇到版本冲突,请检查:

  1. 全局工具中不同版本:dotnet tool list -g
  2. 多个工具清单:在父目录中查找 .config/dotnet-tools.json

清除工具缓存

# 清除 NuGet 工具缓存
dotnet nuget locals all --clear

# 重新恢复工具
dotnet tool restore

示例:完整的开发环境配置

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "docfx": {
      "version": "2.78.3",
      "commands": ["docfx"],
      "rollForward": false
    },
    "dotnet-ef": {
      "version": "9.0.0",
      "commands": ["dotnet-ef"],
      "rollForward": false
    },
    "dotnet-reportgenerator-globaltool": {
      "version": "5.4.1",
      "commands": ["reportgenerator"],
      "rollForward": false
    },
    "csharpier": {
      "version": "0.30.3",
      "commands": ["dotnet-csharpier"],
      "rollForward": false
    },
    "incrementalist.cmd": {
      "version": "1.2.0",
      "commands": ["incrementalist"],
      "rollForward": false
    }
  }
}

开发工作流程:

# 初始设置
dotnet tool restore

# 提交前格式化代码
dotnet csharpier .

# 运行测试并收集覆盖率
dotnet test --collect:"XPlat Code Coverage"
dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coverage

# 构建文档
dotnet docfx docs/docfx.json

# 检查哪些项目更改了(用于大型仓库)
incrementalist --branch main

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

akka-hosting-actor-patterns

Patterns for building entity actors with Akka.Hosting - GenericChildPerEntityParent, message extractors, cluster sharding abstraction, akka-reminders, and ITimeProvider. Supports both local testing and clustered production modes.

akka-net-aspire-configuration

Configure Akka.NET with .NET Aspire for local development and production deployments. Covers actor system setup, clustering, persistence, Akka.Management integration, and Aspire orchestration patterns.

akka-net-best-practices

Critical Akka.NET best practices including EventStream vs DistributedPubSub, supervision strategies, error handling, Props vs DependencyResolver, work distribution patterns, and cluster/local mode abstractions for testability.

akka-net-management

Akka.Management for cluster bootstrapping, service discovery (Kubernetes, Azure, Config), health checks, and dynamic cluster formation without static seed nodes.

akka-net-testing-patterns

Write unit and integration tests for Akka.NET actors using modern Akka.Hosting.TestKit patterns. Covers dependency injection, TestProbes, persistence testing, and actor interaction verification. Includes guidance on when to use traditional TestKit.

api-design

Design stable, compatible public APIs using extend-only design principles. Manage API compatibility, wire compatibility, and versioning for NuGet packages and distributed systems.

aspire-configuration

Configure Aspire AppHost to emit explicit app config via environment variables; keep app code free of Aspire clients and service discovery.

aspire-integration-testing

Write integration tests using .NET Aspire

aspire-service-defaults

Create a shared ServiceDefaults project for Aspire applications. Centralizes OpenTelemetry, health checks, resilience, and service discovery configuration across all services.

crap-analysis

Analyze code coverage and CRAP (Change Risk Anti-Patterns) scores to identify high-risk code. Use OpenCover format with ReportGenerator for Risk Hotspots showing cyclomatic complexity and untested code paths.

csharp-concurrency-patterns

Choosing the right concurrency abstraction in .NET - from async/await for I/O to Channels for producer/consumer to Akka.NET for stateful entity management. Avoid locks and manual synchronization unless absolutely necessary.

database-performance

Database access patterns for performance. Separate read/write models, avoid N+1 queries, use AsNoTracking, apply row limits, and never do application-side joins. Works with EF Core and Dapper.

dependency-injection-patterns

Organize DI registrations using IServiceCollection extension methods. Group related services into composable Add* methods for clean Program.cs and reusable configuration in tests.

dotnet-devcert-trust

Diagnose and fix .NET HTTPS dev certificate trust issues on Linux. Covers the full certificate lifecycle from generation to system CA bundle inclusion, with distro-specific guidance for Ubuntu, Fedora, Arch, and WSL2.

dotnet-project-structure

Modern .NET project structure including .slnx solution format, Directory.Build.props, central package management, SourceLink, version management with RELEASE_NOTES.md, and SDK pinning with global.json.

dotnet-slopwatch

Use Slopwatch to detect LLM reward hacking in .NET code changes. Run after every code modification to catch disabled tests, suppressed warnings, empty catch blocks, and other shortcuts that mask real problems.

efcore-patterns

Entity Framework Core best practices including NoTracking by default, query splitting for navigation collections, migration management, dedicated migration services, and common pitfalls to avoid.

ilspy-decompile

Understand implementation details of .NET code by decompiling assemblies. Use when you want to see how a .NET API works internally, inspect NuGet package source, view framework implementation, or understand compiled .NET binaries.

mailpit-integration

Test email sending locally using Mailpit with .NET Aspire. Captures all outgoing emails without sending them. View rendered HTML, inspect headers, and verify delivery in integration tests.

marketplace-publishing

Workflow for publishing skills and agents to the dotnet-skills Claude Code marketplace. Covers adding new content, updating plugin.json, validation, and release tagging.

相关技能