将 ASP.NET Core 应用部署到 Azure 应用服务Deploy ASP.NET Core apps to Azure App Service

Azure 应用服务是一个用于托管 Web 应用(包括 ASP.NET Core)的 Microsoft 云计算平台服务Azure App Service is a Microsoft cloud computing platform service for hosting web apps, including ASP.NET Core.

有用资源Useful resources

Azure 应用文档、教程、示例、操作指南和其他资源由应用服务文档提供。App Service Documentation is the home for Azure Apps documentation, tutorials, samples, how-to guides, and other resources. 两个有关托管 ASP.NET Core 应用的重要教程为:Two notable tutorials that pertain to hosting ASP.NET Core apps are:

在 Azure 中创建 ASP.NET Core Web 应用Create an ASP.NET Core web app in Azure
使用 Visual Studio 创建 ASP.NET Core Web 应用,并将其部署到 Windows 上的 Azure 应用服务。Use Visual Studio to create and deploy an ASP.NET Core web app to Azure App Service on Windows.

在 Linux 上的应用服务中创建 ASP.NET Core 应用Create an ASP.NET Core app in App Service on Linux
使用命令行创建 ASP.NET Core Web 应用,并将其部署到 Linux 上的 Azure 应用服务。Use the command line to create and deploy an ASP.NET Core web app to Azure App Service on Linux.

ASP.NET Core 文档中提供以下文章:The following articles are available in ASP.NET Core documentation:

使用 Visual Studio 将 ASP.NET Core 应用发布到 Azure
了解如何使用 Visual Studio 将 ASP.NET Core 应用发布到 Azure 应用服务。Learn how to publish an ASP.NET Core app to Azure App Service using Visual Studio.

使用 Visual Studio 和 Git 将 ASP.NET Core 持续部署到 Azure
了解如何使用 Visual Studio 创建 ASP.NET Core Web 应用并使用 Git 将它部署到 Azure 应用服务以实现持续部署。Learn how to create an ASP.NET Core web app using Visual Studio and deploy it to Azure App Service using Git for continuous deployment.

创建第一个管道Create your first pipeline
为 ASP.NET Core 应用设置 CI 生成,然后创建针对 Azure 应用服务的持续部署发布。Set up a CI build for an ASP.NET Core app, then create a continuous deployment release to Azure App Service.

Azure Web 应用沙盒Azure Web App sandbox
探索 Azure Apps 平台强制实施的 Azure 应用服务运行时执行限制。Discover Azure App Service runtime execution limitations enforced by the Azure Apps platform.

应用程序配置Application configuration

PlatformPlatform

64 位 (x64) 和 32 位 (x86) 应用的运行时存在于 Azure 应用服务上。Runtimes for 64-bit (x64) and 32-bit (x86) apps are present on Azure App Service. 应用服务上提供的 .NET Core SDK 为 32 位,但可以使用 Kudu 控制台或通过使用 Visual Studio 发布配置文件或 CLI 命令的 MSDeploy 来部署 64 位应用。The .NET Core SDK available on App Service is 32-bit, but you can deploy 64-bit apps using the Kudu console or via MSDeploy with a Visual Studio publish profile or CLI command.

对于具有本机依赖项的应用,32 位 (x86) 应用的运行时存在于 Azure 应用服务上。For apps with native dependencies, runtimes for 32-bit (x86) apps are present on Azure App Service. 应用服务上提供的 .NET Core SDK 为 32 位。The .NET Core SDK available on App Service is 32-bit.

Packages

包含以下 NuGet 包,以便为部署到 Azure 应用服务的应用提供自动日志记录功能:Include the following NuGet packages to provide automatic logging features for apps deployed to Azure App Service:

Microsoft.AspNetCore.App 元包中不提供前述包。The preceding packages aren't available from the Microsoft.AspNetCore.App metapackage. 面向 .NET Framework 或引用 Microsoft.AspNetCore.App 元包的应用必须显式引用应用的项目文件中的各个包。Apps that target .NET Framework or reference the Microsoft.AspNetCore.App metapackage must explicitly reference the individual packages in the app's project file.

使用 Azure 门户重写应用配置Override app configuration using the Azure Portal

Azure 门户中的应用设置允许为应用设置环境变量。App settings in the Azure Portal permit you to set environment variables for the app. 可以通过环境变量配置提供程序来使用环境变量。Environment variables can be consumed by the Environment Variables Configuration Provider.

在 Azure 门户中创建或修改应用设置并选择“保存”按钮时,Azure 应用将重启。When an app setting is created or modified in the Azure Portal and the Save button is selected, the Azure App is restarted. 服务重启后,应用即可使用环境变量。The environment variable is available to the app after the service restarts.

当应用使用 Web 主机并使用 WebHost.CreateDefaultBuilder 构建主机时,配置主机的环境变量使用 ASPNETCORE_ 前缀。When an app uses the Web Host and builds the host using WebHost.CreateDefaultBuilder, environment variables that configure the host use the ASPNETCORE_ prefix. 有关详细信息,请参阅 ASP.NET Core Web 主机环境变量配置提供程序For more information, see ASP.NET Core Web 主机 and the Environment Variables Configuration Provider.

当应用使用通用主机时,环境变量在默认情况下不会加载到应用的配置,且配置提供程序必须由开发人员添加。When an app uses the Generic Host, environment variables aren't loaded into an app's configuration by default and the configuration provider must be added by the developer. 在添加配置提供程序时,开发人员将确定环境变量前缀。The developer determines the environment variable prefix when the configuration provider is added. 有关详细信息,请参阅 .NET 通用主机环境变量配置提供程序For more information, see .NET 通用主机 and the Environment Variables Configuration Provider.

代理服务器和负载均衡器方案Proxy server and load balancer scenarios

托管在进程外时配置转接头中间件的 IIS 集成中间件和 ASP.NET Core 模块将配置为转发方案 (HTTP/HTTPS) 和发出请求的远程 IP 地址。The IIS Integration Middleware, which configures Forwarded Headers Middleware when hosting out-of-process, and the ASP.NET Core Module are configured to forward the scheme (HTTP/HTTPS) and the remote IP address where the request originated. 对于托管在其他代理服务器和负载均衡器后方的应用,可能需要附加配置。Additional configuration might be required for apps hosted behind additional proxy servers and load balancers. 有关详细信息,请参阅配置 ASP.NET Core 以使用代理服务器和负载均衡器For more information, see Configure ASP.NET Core to work with proxy servers and load balancers.

监视和日志记录Monitoring and logging

部署到应用服务的 ASP.NET Core 应用会自动接收“ASP.NET Core 记录集成”这一应用服务扩展。ASP.NET Core apps deployed to App Service automatically receive an App Service extension, ASP.NET Core Logging Integration. 借助该扩展,可记录 Azure 应用服务上针对 ASP.NET Core 应用的集成。The extension enables logging integration for ASP.NET Core apps on Azure App Service.

部署到应用服务的 ASP.NET Core 应用自动接收应用服务扩展“ASP.NET Core 日志记录扩展”。ASP.NET Core apps deployed to App Service automatically receive an App Service extension, ASP.NET Core Logging Extensions. 借助该扩展,可记录 Azure 应用服务上针对 ASP.NET Core 应用的集成。The extension enables logging integration for ASP.NET Core apps on Azure App Service.

有关监视、日志记录和故障排除的信息,请参阅以下文章:For monitoring, logging, and troubleshooting information, see the following articles:

监视 Azure 应用服务中的应用Monitor apps in Azure App Service
了解如何查看应用和应用服务计划的配额和指标。Learn how to review quotas and metrics for apps and App Service plans.

在 Azure 应用服务中为应用启用诊断日志记录Enable diagnostics logging for apps in Azure App Service
了解如何启用和访问 HTTP 状态代码、失败请求和 Web 服务器活动的诊断日志记录。Discover how to enable and access diagnostic logging for HTTP status codes, failed requests, and web server activity.

处理 ASP.NET Core 中的错误
了解在 ASP.NET Core 应用中处理错误的常见方法。Understand common approaches to handling errors in ASP.NET Core apps.

对 Azure 应用服务上的 ASP.NET Core 进行故障排除
了解如何使用 ASP.NET Core 应用诊断 Azure 应用服务部署问题。Learn how to diagnose issues with Azure App Service deployments with ASP.NET Core apps.

Azure 应用服务和 IIS 上 ASP.NET Core 的常见错误参考
使用故障排除建议查看 Azure 应用服务/IIS 托管的应用的常见部署配置错误。See the common deployment configuration errors for apps hosted by Azure App Service/IIS with troubleshooting advice.

数据保护密钥环和部署槽位Data Protection key ring and deployment slots

数据保护密钥保存在 %HOME%\ASP.NET\DataProtection-Keys 文件夹中。Data Protection keys are persisted to the %HOME%\ASP.NET\DataProtection-Keys folder. 此文件夹由网络存储提供支持,并跨托管应用的所有计算机同步。This folder is backed by network storage and is synchronized across all machines hosting the app. 密钥不是静态保护的。Keys aren't protected at rest. 此文件夹向单个部署槽位中应用的所有实例提供密钥环。This folder supplies the key ring to all instances of an app in a single deployment slot. 各部署槽位(例如过渡槽和生成槽)不共享密钥环。Separate deployment slots, such as Staging and Production, don't share a key ring.

在部署槽位之间交换时,使用数据保护的任意系统都无法使用之前槽位中的密钥环来解密存储的数据。When swapping between deployment slots, any system using data protection won't be able to decrypt stored data using the key ring inside the previous slot. ASP.NET Cookie 中间件使用数据保护来保护其 cookie。ASP.NET Cookie Middleware uses data protection to protect its cookies. 这导致用户注销使用标准 ASP.NET Cookie 中间件的应用。This leads to users being signed out of an app that uses the standard ASP.NET Cookie Middleware. 对于独立于槽位的密钥环解决方案,请使用外部密钥环提供程序,例如:For a slot-independent key ring solution, use an external key ring provider, such as:

  • Azure Blob 存储Azure Blob Storage
  • Azure Key VaultAzure Key Vault
  • SQL 存储SQL store
  • Redis 缓存Redis cache

有关更多信息,请参见在 ASP.NET Core 中的密钥存储提供程序For more information, see 在 ASP.NET Core 中的密钥存储提供程序.

将 ASP.NET Core 预览版部署到 Azure 应用服务Deploy ASP.NET Core preview release to Azure App Service

请使用以下方法之一:Use one of the following approaches:

安装预览站点扩展Install the preview site extension

如果无法使用预览版站点扩展,请提出 aspnet/AspNetCore 问题If a problem occurs using the preview site extension, open an aspnet/AspNetCore issue.

  1. 从 Azure 门户导航到“应用服务”。From the Azure Portal, navigate to the App Service.
  2. 选择 Web 应用。Select the web app.
  3. 在搜索框中键入“ex”以筛选“扩展”,或向下滚动管理工具列表。Type "ex" in the search box to filter for "Extensions" or scroll down the list of management tools.
  4. 选择“扩展”。Select Extensions.
  5. 选择“添加”。Select Add.
  6. 从列表选择“ASP.NET Core {X.Y} ({x64|x86}) 运行时”扩展,其中 {X.Y} 是 ASP.NET Core 预览版本,并且 {x64|x86} 指定平台。Select the ASP.NET Core {X.Y} ({x64|x86}) Runtime extension from the list, where {X.Y} is the ASP.NET Core preview version and {x64|x86} specifies the platform.
  7. 选择“确定”以接受法律条款。Select OK to accept the legal terms.
  8. 选择“确定”安装扩展。Select OK to install the extension.

操作完成时,即表示已安装最新的 .NET Core 预览版。When the operation completes, the latest .NET Core preview is installed. 验证安装:Verify the installation:

  1. 选择“高级工具”。Select Advanced Tools.

  2. 选择“高级工具”中的“Go”。Select Go in Advanced Tools.

  3. 选择“调试控制台” > “PowerShell”菜单项。Select the Debug console > PowerShell menu item.

  4. 从 PowerShell 命令提示符处执行以下命令。At the PowerShell prompt, execute the following command. 在以下命令中,将 ASP.NET Core 运行时版本替换为 {X.Y},并将平台替换为 {PLATFORM}Substitute the ASP.NET Core runtime version for {X.Y} and the platform for {PLATFORM} in the command:

    Test-Path D:\home\SiteExtensions\AspNetCoreRuntime.{X.Y}.{PLATFORM}\
    

    如果安装 x64 预览版运行时,该命令将返回TrueThe command returns True when the x64 preview runtime is installed.

备注

对于在 A 系列计算机上或更高级托管层上托管的应用,可在 Azure 门户中的应用设置中设置应用服务应用的平台体系结构 (x86/x64)。The platform architecture (x86/x64) of an App Services app is set in the app's settings in the Azure Portal for apps that are hosted on an A-series compute or better hosting tier. 如果应用在进程内模式下运行并且平台体系结构配置为 64 位 (x64),则 ASP.NET Core 模块会使用 64 位预览版运行时(如存在)。If the app is run in in-process mode and the platform architecture is configured for 64-bit (x64), the ASP.NET Core Module uses the 64-bit preview runtime, if present. 安装“ASP.NET Core {X.Y} (x64) 运行时”扩展。Install the ASP.NET Core {X.Y} (x64) Runtime extension.

安装 x64 预览版运行时后,在 Kudu PowerShell 命令窗口中运行以下命令以验证该安装。After installing the x64 preview runtime, run the following command in the Kudu PowerShell command window to verify the installation. 在以下命令中,将 ASP.NET Core 运行时版本替换为 {X.Y}Substitute the ASP.NET Core runtime version for {X.Y} in the command:

Test-Path D:\home\SiteExtensions\AspNetCoreRuntime.{X.Y}.x64\

如果安装 x64 预览版运行时,该命令将返回TrueThe command returns True when the x64 preview runtime is installed.

备注

ASP.NET Core 扩展可为 Azure 应用服务上的 ASP.NET Core 启用附加功能,例如启用 Azure 日志记录。ASP.NET Core Extensions enables additional functionality for ASP.NET Core on Azure App Services, such as enabling Azure logging. 从 Visual Studio 进行部署时,将自动安装该扩展。The extension is installed automatically when deploying from Visual Studio. 如果未安装该扩展,请为应用安装它。If the extension isn't installed, install it for the app.

通过 ARM 模板使用预览站点扩展Use the preview site extension with an ARM template

如果使用 ARM 模板创建和部署应用,则可使用 siteextensions 资源类型将站点扩展添加到 Web 应用。If an ARM template is used to create and deploy apps, the siteextensions resource type can be used to add the site extension to a web app. 例如:For example:

{
    "type": "siteextensions",
    "name": "AspNetCoreRuntime",
    "apiVersion": "2015-04-01",
    "location": "[resourceGroup().location]",
    "properties": {
        "version": "[parameters('aspnetcoreVersion')]"
    },
    "dependsOn": [
        "[resourceId('Microsoft.Web/Sites', parameters('siteName'))]"
    ]
}

部署自包含应用Deploy the app self-contained

针对预览运行时的自包含部署 (SCD)在部署中承载预览运行时。A self-contained deployment (SCD) that targets a preview runtime carries the preview runtime in the deployment.

部署自包含应用时:When deploying a self-contained app:

使用 Visual Studio 发布Publish from Visual Studio

  1. 从 Visual Studio 工具栏中选择“构建” > “发布{应用程序名称}”。Select Build > Publish {Application Name} from the Visual Studio toolbar.
  2. 在“选择发布目标”对话框中,确认已选中“应用服务”。In the Pick a publish target dialog, confirm that App Service is selected.
  3. 选择“高级”。Select Advanced. 随即会打开“发布”对话框。The Publish dialog opens.
  4. 在“发布”对话框中:In the Publish dialog:
    • 确认已选中“发布”配置。Confirm that the Release configuration is selected.
    • 打开“部署模式”下拉列表,然后选择“自包含”。Open the Deployment Mode drop-down list and select Self-Contained.
    • 从“目标运行时”下拉列表中选择目标运行时。Select the target runtime from the Target Runtime drop-down list. 默认值为 win-x86The default is win-x86.
    • 如果需要在部署时删除其他文件,请打开“文件发布选项”,然后选中复选框以删除目标位置的其他文件。If you need to remove additional files upon deployment, open File Publish Options and select the check box to remove additional files at the destination.
    • 选择“保存”。Select Save.
  5. 按照发布向导的其余提示创建新站点或更新现有站点。Create a new site or update an existing site by following the remaining prompts of the publish wizard.

使用命令行接口 (CLI) 工具发布Publish using command-line interface (CLI) tools

  1. 在项目文件中,指定一个或多个运行时标识符 (RID)In the project file, specify one or more Runtime Identifiers (RIDs). 对单个 RID 使用 <RuntimeIdentifier>(单数),或使用 <RuntimeIdentifiers>(复数)提供以分号分隔的 RID 列表。Use <RuntimeIdentifier> (singular) for a single RID, or use <RuntimeIdentifiers> (plural) to provide a semicolon-delimited list of RIDs. 在以下示例中,指定 win-x86 RID:In the following example, the win-x86 RID is specified:

    <PropertyGroup>
      <TargetFramework>netcoreapp2.1</TargetFramework>
      <RuntimeIdentifier>win-x86</RuntimeIdentifier>
    </PropertyGroup>
    
  2. 在命令 shell 中,使用 dotnet publish 命令在主机运行时的发布配置中发布应用。From a command shell, publish the app in Release configuration for the host's runtime with the dotnet publish command. 在以下示例中,为 win-x86 RID 发布应用。In the following example, the app is published for the win-x86 RID. 提供给 --runtime 选项的 RID 必须在项目文件的 <RuntimeIdentifier>(或 <RuntimeIdentifiers>)属性中提供。The RID supplied to the --runtime option must be provided in the <RuntimeIdentifier> (or <RuntimeIdentifiers>) property in the project file.

    dotnet publish --configuration Release --runtime win-x86
    
  3. 将 bin/Release/{TARGET FRAMEWORK}/{RUNTIME IDENTIFIER}/publish 目录的内容移动到应用服务中的站点。Move the contents of the bin/Release/{TARGET FRAMEWORK}/{RUNTIME IDENTIFIER}/publish directory to the site in App Service.

对用于容器的 Web 应用使用 DockerUse Docker with Web Apps for containers

Docker 中心包含最新的预览 Docker 映像。The Docker Hub contains the latest preview Docker images. 这些映像可以用作基础映像。The images can be used as a base image. 按常规方法使用映像并部署到用于容器的 Web 应用。Use the image and deploy to Web Apps for Containers normally.

协议设置 (HTTPS)Protocol settings (HTTPS)

借助安全的协议绑定,可在通过 HTTPS 响应请求时指定要使用的证书。Secure protocol bindings allow you specify a certificate to use when responding to requests over HTTPS. 若要绑定,需要一个为特定主机名颁发的有效专用证书 (.pfx)。Binding requires a valid private certificate (.pfx) issued for the specific hostname. 有关详细信息,请参阅教程:将现有自定义 SSL 证书绑定到 Azure 应用服务For more information, see Tutorial: Bind an existing custom SSL certificate to Azure App Service.

转换 web.configTransform web.config

如果需要在发布时转换 web.config(例如,基于配置、配置文件或环境设置环境变量),请参阅 转换 web.configIf you need to transform web.config on publish (for example, set environment variables based on the configuration, profile, or environment), see 转换 web.config.

其他资源Additional resources

Windows Server 上的 Azure 应用服务使用 Internet Information Services (IIS)Azure App Service on Windows Server uses Internet Information Services (IIS). 以下是基础 IIS 技术的相关主题:The following topics pertain to the underlying IIS technology: