

AWS App Runner 从 2026 年 4 月 30 日起，将不再向新客户开放。如果您想使用 App Runner，请在该日期之前注册。现有客户可以继续正常使用该服务。有关更多信息，请参阅 [AWS App Runner 可用性变更](https://docs.aws.amazon.com/apprunner/latest/dg/apprunner-availability-change.html)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 .NET 平台
<a name="service-source-code-net6"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **.NET 6** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

 AWS App Runner .NET 平台提供托管运行时。每个运行时都可以轻松构建和运行带有基于.NET 版本的 Web 应用程序的容器。当你使用.NET 运行时时，App Runner 从托管的.NET 运行时镜像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含适用于.NET 版本的运行时包以及一些工具和常用的依赖包。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

有关有效的.NET 运行时名称和版本，请参阅[.NET 运行时版本信息](service-source-code-dotnet-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

.NET 运行时的版本语法：`major[.minor[.patch]]`

例如：`6.0.9`

以下示例演示了版本锁定：
+ `6.0`— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。
+ `6.0.9`— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

**Topics**
+ [.NET 运行时配置](#service-source-code-net6.config)
+ [NET 运行时示例](#service-source-code-net6.examples)
+ [.NET 运行时版本信息](service-source-code-dotnet-releases.md)

## .NET 运行时配置
<a name="service-source-code-net6.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API** — 调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

## NET 运行时示例
<a name="service-source-code-net6.examples"></a>

以下示例显示了用于构建和运行.NET 服务的 App Runner 配置文件。最后一个示例是完整的.NET 应用程序的源代码，您可以将其部署到.NET 运行时服务。

**注意**  
这些示例中使用的运行时版本是*6.0.9*。您可以将其替换为要使用的版本。有关支持的最新.NET 运行时版本，请参阅[.NET 运行时版本信息](service-source-code-dotnet-releases.md)。

### 最小的.NET 配置文件
<a name="service-source-code-net6.examples.minimal"></a>

此示例显示了可在.NET 托管运行时中使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设，请参阅[配置文件示例](config-file-examples.md#config-file-examples.managed)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: dotnet6
build:
  commands:    
    build:
      - dotnet publish -c Release -o out
run:                              
  command: dotnet out/HelloWorldDotNetApp.dll
```

### 扩展的.NET 配置文件
<a name="service-source-code-net6.examples.extended"></a>

此示例说明如何使用.NET 托管运行时的所有配置密钥。

**注意**  
这些示例中使用的运行时版本是*6.0.9*。您可以将其替换为要使用的版本。有关支持的最新.NET 运行时版本，请参阅[.NET 运行时版本信息](service-source-code-dotnet-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: dotnet6
build:
  commands:
    pre-build:
      - scripts/prebuild.sh
    build:
      - dotnet publish -c Release -o out
    post-build:
      - scripts/postbuild.sh
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"    
run:
  runtime-version: 6.0.9
  command: dotnet out/HelloWorldDotNetApp.dll
  network:
    port: 5000
    env: APP_PORT
  env:
    - name: ASPNETCORE_URLS
      value: "http://*:5000"
```

### 完整的.NET 应用程序源代码
<a name="service-source-code-net6.examples.end2end"></a>

此示例显示了可以部署到.NET 运行时服务的完整.NET 应用程序的源代码。

**注意**  
 运行以下命令创建一个简单的.NET 6 Web 应用程序：` dotnet new web --name HelloWorldDotNetApp -f net6.0`
 将添加到创建的 `apprunner.yaml` .NET 6 Web 应用程序中。

**Example HelloWorldDotNetApp**  

```
version: 1.0
runtime: dotnet6
build:
  commands:
    build:
      - dotnet publish -c Release -o out
run:
  command: dotnet out/HelloWorldDotNetApp.dll
  network:
    port: 5000
    env: APP_PORT
  env:
    - name: ASPNETCORE_URLS
      value: "http://*:5000"
```