共用方式為


Aspire 和 GitHub Codespaces

GitHub Codespaces 提供以 為基礎 Visual Studio Code的雲端裝載開發環境。 它可以直接從網頁瀏覽器或透過 Visual Studio Code 本機存取,其 Visual Studio Code 可作為連線到雲端裝載後端的用戶端。 使用 Aspire 9.1 時,邏輯可更妥善地支援 GitHub Codespace,包括:

  • 使用正確的通訊協議自動設定埠轉送。
  • 自動翻譯儀表板中的 Aspire URL。

在 9.1 之前 Aspire ,它仍然可以在 Aspire Codespace 中使用 GitHub ,但需要更多的手動設定。

GitHub Codespaces 與 Dev Containers

GitHubCodespaces 建置在 和Visual Studio Code上。 除了支援 GitHub Codespaces 之外, Aspire 9.1 還增強對使用 Visual Studio Code 和本機裝載開發容器的支援。 雖然體驗很類似,但有一些差異。 如需詳細資訊,請參閱 Aspire 和 Visual Studio Code 開發容器

使用範本存放庫的快速入門

若要設定 GitHub 的 Codespaces Aspire,請使用存放庫中的 .devcontainer/devcontainer.json 檔案。 最簡單的開始使用方式是從 範本存放庫建立新的存放庫。 請考慮下列步驟:

  1. 使用我們的範本建立新的存放庫

    建立新的存放庫。

    提供詳細數據並選取 [ 建立存放庫] 之後,就會建立存放庫並顯示在 中 GitHub。

  2. 從新的存放庫,選取 [程序代碼] 按鈕,然後選取 [Codespaces] 索引卷標,然後選取 [在 main 上建立程式碼空間]。

    建立程式碼空間

    選取 [ 在主要上建立 codespace] 之後,您可以流覽至 Web 型版本 Visual Studio Code的 。 在使用 Codespace 之前,需要準備容器化開發環境。 此程式會自動在伺服器上發生,您可以選取瀏覽器視窗右下角通知上的 [建置程式碼空間] 連結來檢閱進度。

    建置程式代碼空間

    當容器映像完成建置時, 終端 機提示會出現,表示環境已準備好與其互動。

    Codespace 終端機提示

    此時, Aspire 範本已安裝,開發人員 ASP.NET Core 憑證已新增並接受。

  3. 使用入門範本建立新 Aspire 專案。

    dotnet new aspire-starter --name HelloAspire
    

    這會導致在儲存庫中建立許多檔案和資料夾,這些檔案和資料夾會顯示在視窗左側的 「檔案總管」 面板中。

    [Codespaces 總管] 面板

  4. 透過 HelloAspire.AppHost/AppHost.cs 檔案啟動 AppHost,方法是選取索引標籤列右上角附近的 [執行專案] 按鈕。

    在 Codespace 中啟動 AppHost

    在幾分鐘后,[偵錯控制台] 面板隨即顯示,並且包含具有驗證令牌之 Codespaces 端點上Aspire所公開儀錶板的連結GitHub。

    Codespaces 偵錯控制台

  5. 請在偵錯主控台中選取儀表板的 URL 以開啟儀表板。 這會在瀏覽器中的新索引標籤中開啟 Aspire 儀表板。

    您會在儀錶板上注意到,資源上定義的所有 HTTP/HTTPS 端點都有其一般 localhost 位址轉譯為網域上 app.github.dev 唯一的完整子域。

    Codespaces 翻譯的 URL

    這些端點的流量會自動轉送至在 Codespace 內執行的基礎進程或容器。 這包括 PgAdmin 和 Redis Insight 等開發時間工具。

    備註

    除了內嵌在 [ 偵錯控制台] 儀錶板連結 URL 內的驗證令牌之外,端點也需要透過您的 GitHub 身分識別進行驗證,以避免所有人都能存取埠轉送端點。 如需 Codespaces 中 GitHub 埠轉送的詳細資訊,請參閱 程式代碼空間中的轉送埠

  6. 認可存放庫的 GitHub 變更。

    GitHub Codespaces 不會自動將變更認可至您在 中 GitHub處理的分支。 您必須使用 [原始檔控制面板 ] 來暫存並認可變更,並將其推送回存放庫。

    在 Codespace 中 GitHub 工作類似於在您自己的電腦上使用 Visual Studio Code 。 您可以像往常一樣簽出不同的分支並推送更改。 此外,如果您想要快速處理另一個分支,而不會中斷現有的偵錯工作階段,您可以輕鬆地同時啟動多個 Codespaces。 如需詳細資訊,請參閱 在 codespace 中開發

  7. 清除您的 Codespace。

    GitHub Codespace 是暫時的開發環境,雖然您可能會長時間使用,但它們應該被視為您視需要重新建立的一次性資源 (所有自訂/設定都包含在 devcontainer.json 和相關聯的組態檔中)。

    若要刪除 GitHub Codespace,請流覽 GitHub Codespaces 頁面。 這會顯示所有 Codespaces 的清單。 您可以從這裡對每個 Codespace 執行管理作業,包括刪除它們。

    GitHub 使用 Codespaces 的費用。 如需詳細資訊,請參閱管理組織中 Codespaces 的成本GitHub

    備註

    Aspire支援在與 Codespaces 無關Visual Studio Code的GitHub中使用開發容器。 如需如何在本機使用開發容器的詳細資訊,請參閱 Aspire 和 中的Visual Studio Code開發容器

手動設定 devcontainer.json

上述逐步解說示範如何使用 Devcontainer 範本建立 GitHub Codespace Aspire 的簡化程式。 如果您已經有現有的存放庫,並希望使用 Aspire使用 Devcontainer 功能,請將 devcontainer.json 檔案新增至存放庫內的 .devcontainer 資料夾:

└───📂 .devcontainer
     └─── devcontainer.json

範本儲存庫包含 devcontainer.json 檔案的複本,您可以將其用作起點,這應該足以用於 Aspire。 下列 JSON 代表範本中最新版的 .devcontainer/devcontainer.json 檔案:

// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/dotnet
{
    "name": "Aspire",
    // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
    "image": "mcr.microsoft.com/devcontainers/dotnet:dev-10.0-noble",
    "features": {
        "ghcr.io/devcontainers/features/docker-in-docker:2": {},
        "ghcr.io/devcontainers/features/powershell:1": {}
    },

    "hostRequirements": {
        "cpus": 8,
        "memory": "32gb",
        "storage": "64gb"
    },

    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [5000, 5001],
    // "portsAttributes": {
    //		"5001": {
    //			"protocol": "https"
    //		}
    // }

    // Use 'postCreateCommand' to run commands after the container is created.
    // "postCreateCommand": "dotnet restore",
    "onCreateCommand": "curl -sSL https://aspire.dev/install.sh | bash",
    "postStartCommand": "dotnet dev-certs https --trust",
    "customizations": {
        "vscode": {
            "extensions": [
                "ms-dotnettools.csdevkit",
                "GitHub.copilot-chat",
                "GitHub.copilot"
            ]
        }
    }
    // Configure tool-specific properties.
    // "customizations": {},

    // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
    // "remoteUser": "root"
}

加速 Codespace 建立

建立 Codespace 可能需要 GitHub 一些時間,因為它會準備基礎容器映像。 為了加快此過程,您可以利用 預建置將 建立時間大幅縮短至大約 30-60 秒(確切的時間可能會有所不同)。 如需 Codespaces 預先建置的詳細資訊 GitHub ,請參閱 GitHub Codespaces 預先建置