GitHub Codespaces 提供基于 Visual Studio Code云托管的开发环境。 可以直接从 Web 浏览器或通过 Visual Studio Code 本地访问它,该 Visual Studio Code 浏览器充当连接到云托管后端的客户端。 使用 Aspire 9.1 时,逻辑可以更好地支持 GitHub Codespaces,包括:
- 使用正确的协议自动配置端口转发。
- 自动翻译 Aspire 仪表板中的 URL。
在 9.1 之前 Aspire ,仍可以在 Aspire Codespace 中使用 GitHub ,但需要更多手动配置。
GitHub Codespaces 与开发容器
GitHubCodespaces 基于Visual Studio Code。 除了支持 GitHub Codespaces 之外, Aspire 9.1 还增强了对使用 Visual Studio Code 和本地托管开发容器的支持。 虽然体验相似,但存在一些差异。 有关详细信息,请参阅 Aspire 和 Visual Studio Code 开发容器。
使用模板存储库快速入门
若要配置 GitHub Codespaces Aspire,请使用存储库中的 .devcontainer/devcontainer.json 文件。 最简单的入门方法是从 模板存储库创建新存储库。 考虑以下步骤:
使用模板创建新存储库。
提供详细信息并选择 “创建存储库”后,将创建并显示 GitHub存储库。
从新存储库中,选择“代码”按钮,然后选择“Codespaces”选项卡,然后选择 “在主”上创建代码空间。
在 main 上选择“创建代码空间”后,导航到基于 Web 的版本 Visual Studio Code。 在使用 Codespace 之前,需要准备好容器化开发环境。 此过程在服务器上自动发生,可以通过选择浏览器窗口右下角通知上的 “生成代码空间 ”链接来查看进度。
容器映像生成完毕后, 将显示终端 提示符,表明环境已准备好与之交互。
此时, Aspire 已安装模板,并且 ASP.NET Core 已添加并接受开发人员证书。
使用初学者模板创建新 Aspire 项目。
dotnet new aspire-starter --name HelloAspire这会导致在存储库中创建许多文件和文件夹,这些文件和文件夹在窗口左侧的 资源管理器 面板中可见。
通过 HelloAspire.AppHost/AppHost.cs 文件启动 AppHost,方法是选择选项卡栏右上角附近的“运行项目”按钮。
片刻后,将显示“调试控制台”面板,其中包含一个指向使用身份验证令牌的 Codespaces 终结点上公开的Aspire仪表板的链接GitHub。
在调试控制台中选择仪表板 URL 来打开仪表板Aspire。 这会在浏览器中的单独选项卡中打开 Aspire 仪表板。
在仪表板上可以看到,在资源上定义的所有 HTTP/HTTPS 终结点已将其典型
localhost地址转换为域上app.github.dev唯一的完全限定子域。发送到每个终结点的流量会自动转发到 Codespace 中运行的基础进程或容器。 这包括 PgAdmin 和 Redis Insight 等开发时间工具。
注释
除了嵌入调试 控制台仪表板链接 URL 中的身份验证令牌外,终结点还需要通过 GitHub 标识进行身份验证,以避免每个人都可以访问端口转发的终结点。 有关 Codespaces 中的 GitHub 端口转发的详细信息,请参阅 代码空间中的转发端口。
将更改提交到 GitHub 存储库。
GitHub Codespaces 不会自动将更改提交到正在使用 GitHub的分支。 必须使用 “源代码管理 ”面板暂存和提交更改并将其推送回存储库。
在 Codespace 中 GitHub 工作类似于在自己的计算机上使用 Visual Studio Code 。 可以签出不同的分支并推送更改,就像平时一样。 此外,如果想要快速处理另一个分支而不中断现有调试会话,可以轻松同时启动多个 Codespace。 有关详细信息,请参阅 在代码空间中开发。
清理 Codespace。
GitHub 代码空间是临时开发环境,虽然你可能在较长时间内使用一个,但它们应被视为根据需要重新创建的一次性资源(其中包含 devcontainer.json 和关联的配置文件中包含的所有自定义/设置)。
若要删除 GitHub Codespace,请访问 GitHub Codespaces 页。 这会显示所有 Codespaces 的列表。 在这里,你可以对每个 Codespace 执行管理作,包括删除它们。
GitHub 使用 Codespaces 的费用。 有关详细信息,请参阅管理组织中的 Codespaces 成本GitHub。
注释
Aspire支持独立于 Visual Studio Code Codespaces 使用开发容器GitHub。 有关如何在本地使用开发容器的详细信息,请参阅Aspire和开发容器。Visual Studio Code
手动配置 devcontainer.json
前面的演练演示了使用 GitHub Devcontainer 模板创建 Aspire Codespace 的简化过程。 如果已有一个存储库,并且想要将 Devcontainer 功能用于 Aspire其中,请将 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": ".NET Aspire",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"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 创建速度
GitHub创建 Codespace 可能需要一些时间,因为它准备基础容器映像。 为了加快此过程,可以利用 预生成 显著缩短创建时间约 30-60 秒(确切的计时可能会有所不同)。 有关 Codespaces 预生成的详细信息 GitHub ,请参阅 GitHub Codespaces 预生成。