使用 GitHub Copilot 對 .NET 應用程式進行現代化升級

GitHub Copilot 現代化是一個由 AI 驅動的代理程式,能將 .NET 專案升級至最新版本,並將應用程式遷移至 Azure。 本文將引導你如何透過結構化的三階段工作流程:評估、規劃與執行,升級你的 .NET 應用程式。

現代化代理會分析您的專案與相依性,在每個階段建立詳細的升級文件,並在整個過程中協助程式碼修正。 該代理程式支援從舊版 .NET 升級到最新版本,包括從 .NET Framework 升級到現代版 .NET。

先決條件

在開始升級前,先在你的開發環境中設定 GitHub Copilot 現代化。 安裝步驟請參見 Install GitHub Copilot modernization

啟動升級

要開始升級,請使用 Copilot 中的 modernize-dotnet 代理:

  1. 在你的開發環境中開啟你的 .NET 專案或解決方案。

  2. 啟動代理人時,請使用以下其中一種方法:

    • Visual Studio:右鍵點擊 方案總管 中的解決方案或專案,選擇 Modernize。 或者,打開 GitHub Copilot Chat 視窗,輸入 @Modernize
    • Visual Studio Code:打開 GitHub Copilot Chat面板並輸入 @modernize-dotnet
    • GitHub Copilot CLI:輸入 @modernize-dotnet,接著輸入您的升級或遷移請求。
    • GitHub.com:使用你倉庫中的 modernize-dotnet 編碼代理程式。
  3. 告訴客服人員要升級或遷移什麼。

當你開始升級時,Copilot 會收集預初始化資訊:目標框架版本、Git 分支策略,以及工作流程模式(自動或由你引導)。 接著Copilot評估你的專案,並執行三階段工作流程,在你的倉庫中為每個階段撰寫 .github/upgrades/{scenarioId} 下的 Markdown 檔案。 該 {scenarioId} 值是升級類型的唯一識別碼,例如 dotnet-version-upgrade。 如果之前嘗試已經有.github/upgrades/{scenarioId},Copilot會詢問是否繼續或重新開始。

三個階段分別是:

  • 評估階段。 Copilot 會檢視你的專案,提出策略決策供你審查,並保存已確認的決策。 請先自訂評估內容再繼續。
  • 規劃階段。 Copilot 會建立詳細的規格,說明達成目標升級的步驟。
  • 處決階段。 Copilot 將計畫拆解成連續任務並執行升級。

回顧評估

評估會檢視你的專案結構、相依性及程式碼模式,以找出需要改變的地方。 Copilot 會自動開始評估,並在 assessment.md 中生成一個 .github/upgrades/{scenarioId} 檔案。

評估列出了破壞性的變更、API 相容性問題、已棄用的模式以及升級範圍。 以下範例展示了一項針對 .NET 6.0 升級至 .NET 10.0 的 ASP.NET Core 專案評估的一部分:

# Projects and dependencies analysis

This document provides a comprehensive overview of the projects and their dependencies in the context of upgrading to .NETCoreApp,Version=v10.0.

## Table of Contents

- [Executive Summary](#executive-Summary)
  - [Highlevel Metrics](#highlevel-metrics)
  - [Projects Compatibility](#projects-compatibility)
  - [Package Compatibility](#package-compatibility)
  - [API Compatibility](#api-compatibility)
- [Aggregate NuGet packages details](#aggregate-nuget-packages-details)
- [Top API Migration Challenges](#top-api-migration-challenges)
  - [Technologies and Features](#technologies-and-features)
  - [Most Frequent API Issues](#most-frequent-api-issues)
- [Projects Relationship Graph](#projects-relationship-graph)
- [Project Details](#project-details)

  - [MvcMovie.Tests\MvcMovie.Tests.csproj](#mvcmovietestsmvcmovietestscsproj)
  - [MvcMovie\MvcMovie.csproj](#mvcmoviemvcmoviecsproj)
  - [RazorMovie.Tests\RazorMovie.Tests.csproj](#razormovietestsrazormovietestscsproj)
  - [RazorMovie\RazorMovie.csproj](#razormovierazormoviecsproj)
  - [WpfMovie.Tests\WpfMovie.Tests.csproj](#wpfmovietestswpfmovietestscsproj)
  - [WpfMovie\WpfMovie.csproj](#wpfmoviewpfmoviecsproj)

...

要檢視並客製化評估:

  1. 打開 assessment.md 檔案於 .github/upgrades/{scenarioId} 中。
  2. 檢視已識別出的破壞性變更與相容性問題。
  3. 在文件中加入專案特定的背景或關注點。
  4. 告訴Copilot開始進入規劃階段。

檢視升級選項

評估結束後,Copilot 會評估你的解決方案,並提出升級策略決策供你檢視。 代理人會根據你的專案結構建議方案,並將確認的決策保存在 upgrade-options.md.github/upgrades/{scenarioId}

通常選項包括:

  • 升級策略。 自下而上(先處理葉子專案)、自上而下(應用程式優先)或同時一次性完成所有專案。
  • 專案升級方法。 原地重寫或平行升級。
  • 科技現代化。 是否要升級像是 Entity Framework(EF6 到 EF Core)、相依注入、日誌記錄和設定等技術。
  • 套件管理。 是否採用中央套件管理。
  • 相容性處理。 如何處理不支援的 API、不相容的套件,以及平台特定的功能。

檢視建議選項並確認或否決。 告訴Copilot開始進入規劃階段。

開始規劃並檢視計畫

規劃階段會將評估及您確認的升級選項轉化為詳細規格,說明如何解決每個問題。 當你告訴Copilot繼續規劃時,它會產生一個 plan.md 檔案,格式為 .github/upgrades/{scenarioId}。 代理程式也會建立 scenario-instructions.md 一個檔案,儲存偏好設定、決策及升級的自訂指示。

計畫記錄了升級策略、重構方法、相依升級路徑及風險緩解措施。 以下範例展示了 ASP.NET Core 專案的一部分計畫:

# .NET 10 Upgrade Plan

## Table of Contents

- [Executive Summary](#executive-summary)
- [Migration Strategy](#migration-strategy)
- [Detailed Dependency Analysis](#detailed-dependency-analysis)
- [Project-by-Project Plans](#project-by-project-plans)
- ... <removed to save space> ...
- ...

---

## Executive Summary

### Scenario Description
Upgrade all projects in the MvcMovieNet6 solution from .NET 6 to .NET 10 (Long Term Support). The solution contains:
- **RazorMovie**: ASP.NET Core Razor Pages application (primary focus)
- **MvcMovie**: ASP.NET Core MVC application
- **WpfMovie**: Windows Presentation Foundation desktop application
- **3 Test Projects**: Unit test projects for each application

### Scope & Current State
- **6 projects** requiring framework upgrade (net6.0 → net10.0)
- **1,862 total lines of code** across 54 files
- **16 NuGet packages** (6 require updates, 10 compatible)
- **65 identified issues** (1 security vulnerability, 1 deprecated package, 51 WPF API issues, minor behavioral changes)
- **All projects are SDK-style** (modern project format)

...

檢視並調整計畫:

  1. 打開 plan.md 檔案於 .github/upgrades/{scenarioId} 中。

  2. 檢視升級策略與相依性更新。

  3. 根據需要,編輯計畫調整升級步驟或補充背景資訊。

    謹慎

    計畫取決於專案間的相互依賴關係。 如果你修改計畫,導致升級路徑無法完成,升級就不會成功。 例如,如果Project A依賴Project B,而你從升級計畫中移除Project B,升級Project A可能會失敗。

  4. 告訴Copilot進入執行階段。

開始執行並執行升級

執行階段將計畫拆分為連續且具體的任務,並有驗證標準。 當你告訴Copilot繼續執行時,它會產生一個 tasks.md 檔案,格式為 .github/upgrades/{scenarioId}

任務清單描述了每個任務,以及 Copilot 如何驗證成功。 以下範例展示了包含 ASP.NET Core 與 WPF 專案的解決方案任務清單:

# MvcMovieNet6 .NET 10 Upgrade Tasks

## Overview

This document tracks the execution of the MvcMovieNet6 solution upgrade from .NET 6 to .NET 10. All projects will be upgraded simultaneously in a single atomic operation.

**Progress**: 0/3 tasks complete (0%) ![0%](https://progress-bar.xyz/0)

---

## Tasks

### [ ] TASK-001: Verify prerequisites
**References**: Plan §Phase 0

- [ ] (1) Verify .NET 10 SDK installed per Plan §Phase 0
- [ ] (2) .NET 10 SDK meets minimum requirements (**Verify**)

---

### [ ] TASK-002: Atomic framework and package upgrade with compilation fixes
**References**: Plan §Phase 1, Plan §Package Update Reference, Plan §Breaking Changes Catalog, Plan §Project-by-Project Plans

- [ ] (1) Update TargetFramework to net10.0 in MvcMovie.csproj, MvcMovie.Tests.csproj, RazorMovie.csproj, RazorMovie.Tests.csproj per Plan §Phase 1
- [ ] (2) Update TargetFramework to net10.0-windows in WpfMovie.csproj, WpfMovie.Tests.csproj per Plan §Phase 1
- [ ] (3) All project files updated to target framework (**Verify**)
- [ ] (4) Update package references per Plan §Package Update Reference (MvcMovie: EF Core 10.0.1, Code Generation 10.0.0; RazorMovie: HtmlSanitizer 9.0.889 security fix)
- [ ] (5) All package references updated (**Verify**)
- [ ] (6) Restore all dependencies across solution
- [ ] (7) All dependencies restored successfully (**Verify**)
- [ ] (8) Build solution and fix all compilation errors per Plan §Breaking Changes Catalog (focus: BinaryFormatter removal in WpfMovie, WPF control API binary incompatibilities, UseExceptionHandler behavioral changes, HtmlSanitizer API changes)
- [ ] (9) Solution builds with 0 errors (**Verify**)
- [ ] (10) Commit changes with message: "TASK-002: Complete atomic upgrade to .NET 10 (all projects, packages, and compilation fixes)"

---

### [ ] TASK-003: Run full test suite and validate upgrade
**References**: Plan §Phase 2, Plan §Testing & Validation Strategy

- [ ] (1) Run tests in MvcMovie.Tests, RazorMovie.Tests, and WpfMovie.Tests projects
- [ ] (2) Fix any test failures (reference Plan §Breaking Changes Catalog for common issues: HtmlSanitizer behavior, BinaryFormatter replacement, framework behavioral changes)
- [ ] (3) Re-run all tests after fixes
- [ ] (4) All tests pass with 0 failures (**Verify**)
- [ ] (5) Commit test fixes with message: "TASK-003: Complete testing and validation for .NET 10 upgrade"

要進行升級:

  1. 告訴Copilot開始升級.
  2. 透過檢視 tasks.md 檔案來監控進度,Copilot 更新任務狀態。
  3. 如果 Copilot 遇到無法解決的問題,就提供所需的協助。
  4. 根據你的決策與變更,Copilot 會調整策略以適應剩餘任務並持續升級。

Copilot 會根據你在預初始化時設定的 Git 策略提交變更:每個任務、每個任務群組,或是在最後階段。

確認升級

升級完成後,Copilot 會在聊天回覆中顯示建議的下一步步驟。 檢查tasks.md.github/upgrades/{scenarioId}檔案中的每個步驟的狀態。

以下範例顯示 ASP.NET Core 專案升級已完成的任務:

# MvcMovieNet6 .NET 10 Upgrade Tasks

## Overview

This document tracks the execution of the MvcMovieNet6 solution upgrade from .NET 6 to .NET 10. All projects will be upgraded simultaneously in a single atomic operation.

**Progress**: 3/3 tasks complete (100%) ![0%](https://progress-bar.xyz/100)

---

## Tasks

### [✓] TASK-001: Verify prerequisites *(Completed: 2025-12-12 21:09)*
**References**: Plan §Phase 0

- [✓] (1) Verify .NET 10 SDK installed per Plan §Phase 0
- [✓] (2) .NET 10 SDK meets minimum requirements (**Verify**)

...

為了驗證升級:

  1. 請在tasks.md中檢視最終任務狀態。
  2. 處理任何測試失敗或編譯錯誤。
  3. 請確定所有更新的 NuGet 套件都與您的應用程式相容。
  4. 請徹底測試你的應用程式,確認升級是否成功。
  5. 應用升級版 .NET 版本中可用的新功能與改進。