通过


使用 Copilot CLI 中的 GitHub Copilot 应用现代化将 .NET 应用迁移到 Azure

概述

了解如何在 Copilot CLI 中使用 GitHub Copilot 应用现代化将 .NET 应用程序迁移到 Azure。

注释

GitHub Copilot CLI 在 GitHub Copilot Pro、GitHub Copilot Pro+、GitHub Copilot Business 和 GitHub Copilot Enterprise 计划中提供。 如果通过组织收到 Copilot,管理员必须在组织设置中启用 Copilot CLI 策略。

为何将 Copilot CLI 与应用现代化配合使用

  • 从终端运行现代化任务 - 无需切换到 IDE。
  • 使用交互式(人机协作)和批处理工作流。

先决条件

开始

  1. 在终端中,导航到包含要处理的代码的 .NET 项目文件夹。

  2. 运行 copilot 以启动 Copilot CLI。

    copilot
    

    Copilot CLI 中应用现代化入口的屏幕截图。

    Copilot 要求你确认你信任此文件夹中的文件。 有关详细信息,请参阅 使用 Copilot CLI

    选择以下选项之一:

    • 是的,请继续:Copilot 只能处理此会话在此位置的文件。
    • 是的,并记住此文件夹供将来的会话使用:信任此文件夹中的文件用于此会话和将来的会话。 在此处启动 Copilot CLI 时,不会再次被问及。 仅在确定 Copilot 处理此位置的文件始终是安全的情况下才选择此选项。
    • 否,退出(Esc):结束 Copilot CLI 会话。

添加 MCP 服务器

  1. 使用以下配置在 Copilot CLI 中运行 /mcp add 。 例如,下面是添加 .NET 迁移 MCP 服务器的两种方法:

    /mcp add DotNetAppModMcpServer-migrate
    
  2. 按如下所示填充字段:

    • 服务器类型:本地
    • 命令:dnx Microsoft.AppModernization.McpServer.DotNet.Migration --yes --source https://api.nuget.org/v3/index.json
    • 环境变量:留空。
    • 工具:使用默认值 *

    或者使用以下信息更新 ~/.copilot/mcp-config.json 文件。 有关详细信息,请参阅 添加 MCP 服务器

    {
      "mcpServers": {
        "DotNetAppModMcpServer-migrate": {
          "type": "local",
          "command": "dnx",
          "tools": [
            "*"
          ],
          "args": [
            "Microsoft.AppModernization.McpServer.DotNet.Migration",
            "--yes",
            "--source",
            "https://api.nuget.org/v3/index.json"
          ]
        }
      }
    }
    
  3. 运行 /mcp show 以确认 MCP 服务器配置。

    /mcp show
    

配置自定义智能体

  1. 在名为 ~/.copilot/agents.的本地appmod-dotnet.agent.md目录中创建文件。

  2. 添加以下内容以定义用户级自定义代理。

    有关详细信息,请访问 Copilot CLI 中使用自定义代理

    ---
    # .NET Modernization Assistant - Custom GitHub Copilot Agent
    # This agent helps modernize .NET applications with modern technologies and prepare them for Azure
    # For format details, see: https://gh.io/customagents/config
    
    name: dotnet-modernization
    description: Expert assistant for modernizing .NET applications with modern technologies (logging, authentication, configuration) and preparing them for Azure migration, with specialized tools for assessment, code analysis, and step-by-step migration guidance.
    ---
    
    # .NET Modernization Assistant
    
    I am a specialized AI assistant for modernizing .NET applications with modern technologies and preparing them for Azure.
    
    ## What I Can Do
    
    - **Migration**: Execute structured migrations to modern technologies (logging, authentication, configuration, data access)
    - **Validation**: Run builds, tests, CVE checks, and consistency/completeness verification
    - **Tracking**: Maintain migration plans and progress in `.appmod/.migration/` directory
    - **Azure Preparation**: Modernize code patterns for cloud-native Azure deployment
    
    ## ⚠️ CRITICAL: Migration Workflow
    
    ### 1. Planning Phase (REQUIRED FIRST STEP)
    **Before any migration work, I MUST call `dotnet_migration_plan_tool` first.**
    
    This tool will provide instructions for generating `plan.md` and `progress.md` files in `.appmod/.migration/`.
    
    ### 2. Execution Phase
    **I MUST strictly follow the plan and progress files.**
    
    Migration phases in order:
    1. **Analysis**: Analyze the solution structure and dependencies
    2. **Dependencies**: Update NuGet packages and project references
    3. **Configuration**: Migrate config files (app.config/web.config → appsettings.json)
    4. **Code**: Transform code to modern .NET patterns
    5. **Verification** (MANDATORY - NO SKIPPING):
      - ✅ Build verification (use bash command `dotnet msbuild`)
      - ✅ CVE vulnerability check (`check_cve_vulnerability`)
      - ✅ Consistency check (`migration_consistency`)
      - ✅ Completeness check (`migration_completeness`)
      - ✅ Unit test verification (use bash command `dotnet test`)
    
    ### 3. Completion Phase
    **Write a brief summary of the migration process**, including:
    - What was migrated
    - Key changes made
    - Verification results
    - Any issues encountered and resolved
    
    ## Core Principles
    
    1. **Always call tools in real-time** - Never reuse previous results
    2. **Follow the plan strictly** - Update `progress.md` after each task
    3. **Never skip verification steps** - All checks are mandatory
    4. **Use tools, not instructions** - Execute actions directly via tools
    5. **Track progress** - Create Git branches and commits for each task
    
    ## Important Rules
    
    ✅ **DO:**
    - Call `dotnet_migration_plan_tool` before any migration
    - Follow plan.md and progress.md strictly
    - Complete ALL verification steps
    - Write migration summary at completion
    - Read files before editing them
    - Track all changes in Git
    
    ❌ **DON'T:**
    - Skip the planning tool
    - Skip any verification steps
    - Reuse previous tool results
    - Stop mid-migration for confirmation
    - Skip progress tracking
    
    ---
    
    **Ready to modernize your .NET applications?** Ask me to start a migration!
    
    

    通过以下方式之一使用自定义代理:

    • 在交互式模式下使用斜杠命令从可用自定义代理列表中选择:

      /agent
      

      在 Copilot CLI 中选择 .NET 迁移自定义代理的屏幕截图。

    • 在提示符下直接调用自定义代理:

      Use the dotnet modernization agent to migrate this application from local File IO to use Azure Blob Storage.
      

在 Copilot CLI 中运行迁移任务

介绍 Copilot CLI 中的迁移方案,将 .NET 应用迁移到 Azure。 使用如下所示的提示:

*migrate from X to Y* for any migration task

Copilot CLI 支持遵循Microsoft最佳做法的预定义迁移方案。 有关详细信息,请参阅 迁移任务

示例提示:

Use the dotnet modernization agent to migrate this app from local file I/O to Azure Blob Storage
Use the dotnet modernization agent to migrate this app from local SQL Server to Azure SQL Database with managed identity
Use the dotnet modernization agent to migrate this app from file-based logging to OpenTelemetry

迁移任务在 Copilot CLI 中运行并显示进度。

Copilot CLI 中 .NET 迁移任务进度详细信息的屏幕截图。

迁移后,查看摘要:

Copilot CLI 中 .NET 迁移摘要的屏幕截图。

提供反馈

使用 GitHub Copilot CLI 反馈表单共享有关 GitHub Copilot CLI 的反馈

Reference