Community程式設計與開發github.com

dotnet-local-tools

透過 `dotnet-tools.json` 管理本機 .NET 工具,確保不同開發環境與 CI/CD 管線使用一致的工具版本。

dotnet-local-tools 是什麼?

此技能專注於管理 .NET 本機工具。它指導使用者如何在專案層級(而非全域)安裝和還原 .NET CLI 工具,並以 `.config/dotnet-tools.json` 檔案集中管理工具版本。這樣可以避免不同專案之間的全域工具版本衝突,並確保整個開發團隊以及 CI/CD 管線使用完全相同的工具版本,從而實現可重複的建置流程。技能內容詳細說明了如何初始化工具清單、安裝與還原工具、以及管理版本更新。此外,還提供了常見工具(如 DocFX、dotnet-ef、ReportGenerator、CSharpier 等)的安裝與使用範例。同時,也涵蓋了與 GitHub Actions 和 Azure Pipelines 的整合方式,以及最佳實踐(如固定確切版本、在 CI 中先還原工具)和常見問題排解方法。支援的開發人員工具包括任何使用 .NET CLI 的工具,因此適用於所有 .NET 開發環境。

相容平台~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 中說明工具需求

在 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.

相關技能