將 ASP.NET Core 應用程式部署至 Azure App ServiceDeploy ASP.NET Core apps to Azure App Service

Azure App ServiceMicrosoft 雲端運算平台服務,用於裝載 Web 應用程式,包括 ASP.NET Core。Azure App Service is a Microsoft cloud computing platform service for hosting web apps, including ASP.NET Core.

實用資源Useful resources

App Service 文件是 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
在 Windows 上使用 Visual Studio 建立 ASP.NET Core Web 應用程式並將其部署到 Azure App Service。Use Visual Studio to create and deploy an ASP.NET Core web app to Azure App Service on Windows.

在 Linux 上的 App Service 中建立 ASP.NET Core 應用程式Create an ASP.NET Core app in App Service on Linux
在 Linux 上使用命令列建立 ASP.NET Core Web 應用程式並將其部署到 Azure App Service。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 App Service。Learn how to publish an ASP.NET Core app to Azure App Service using Visual Studio.

搭配 ASP.NET Core 使用 Visual Studio 與 Git 持續部署至 Azure
了解如何使用 Visual Studio 建立 ASP.NET Core Web 應用程式,並透過 Git 持續部署將它部署到 Azure App Service。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 App Service。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 應用程式平台強制實施的 Azure App Service 執行階段執行限制。Discover Azure App Service runtime execution limitations enforced by the Azure Apps platform.

應用程式組態Application configuration

PlatformPlatform

Azure App Service 具有 64 位元 (x64) 及 32 位元 (x86) 應用程式的執行階段。Runtimes for 64-bit (x64) and 32-bit (x86) apps are present on Azure App Service. 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.

對於具有原生相依性的應用程式而言,Azure App Service 具有 32 位元 (x86) 應用程式的執行階段。For apps with native dependencies, runtimes for 32-bit (x86) apps are present on Azure App Service. App Service 可使用的 .NET Core SDK 為 32 位元。The .NET Core SDK available on App Service is 32-bit.

封裝Packages

包含下列 NuGet 套件,為部署至 Azure App Service 的應用程式提供自動記錄功能: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 主機並使用會將主機設定為使用 ASPNETCORE_ 前置詞的 WebHost.CreateDefaultBuilder 環境變數來建置主機。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 伺服器和負載平衡器案例Proxy server and load balancer scenarios

用來設定轉送標頭中介軟體及 ASP.NET Core 模組的 IIS Integration 中介軟體會設定為轉送配置 (HTTP/HTTPS) 及發出要求的遠端 IP 位址。The IIS Integration Middleware, which configures Forwarded Headers Middleware, and the ASP.NET Core Module are configured to forward the scheme (HTTP/HTTPS) and the remote IP address where the request originated. 其他 Proxy 伺服器和負載平衡器後方託管的應用程式可能需要其他設定。Additional configuration might be required for apps hosted behind additional proxy servers and load balancers. 如需詳細資訊,請參閱設定 ASP.NET Core 以處理 Proxy 伺服器和負載平衡器For more information, see Configure ASP.NET Core to work with proxy servers and load balancers.

監視與記錄Monitoring and logging

部署到 App Service 的 ASP.NET Core 應用程式會自動接收 App Service 延伸模組:ASP.NET Core 記錄整合ASP.NET Core apps deployed to App Service automatically receive an App Service extension, ASP.NET Core Logging Integration. 延伸模組讓 Azure App Service 上的 ASP.NET Core 應用程式得以進行記錄整合。The extension enables logging integration for ASP.NET Core apps on Azure App Service.

部署到 App Service 的 ASP.NET Core 應用程式會自動接收 App Service 延伸模組 ASP.NET Core 記錄延伸模組ASP.NET Core apps deployed to App Service automatically receive an App Service extension, ASP.NET Core Logging Extensions. 延伸模組讓 Azure App Service 上的 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 App Service 中的應用程式Monitor apps in Azure App Service
了解如何檢閱應用程式和 App Service 方案的配額和計量。Learn how to review quotas and metrics for apps and App Service plans.

為 Azure App Service 中的應用程式啟用診斷記錄Enable diagnostics logging for apps in Azure App Service
探索如何啟用及存取 HTTP 狀態碼、失敗要求和網頁伺服器活動的診斷記錄。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 App Service 上的 ASP.NET Core 進行疑難排解
了解如何診斷使用 ASP.NET Core 應用程式部署 Azure App Service 的問題。Learn how to diagnose issues with Azure App Service deployments with ASP.NET Core apps.

Azure App Service 與 IIS 搭配 ASP.NET Core 時的常見錯誤參考
了解託管於 Azure App Service/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 App ServiceDeploy 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 入口網站瀏覽至 App Service。From the Azure Portal, navigate to the App Service.
  2. 選取 Web 應用程式。Select the web app.
  3. 在搜尋方塊中鍵入 "ex" 來篩選 "Extensions",也可往下捲動管理工具的清單。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. 在 [進階工具] 中選取 [移至]。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 系列計算或更高裝載層的應用程式,應用程式服務應用程式的平台架構 (x86/x64) 會設定在 Azure 入口網站中應用程式的設定內。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. 在 [挑選發佈目標] 對話方塊中,確認已選取 [App Service]。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-x86In the following example, the win-x86 RID is specified:

    <PropertyGroup>
      <TargetFramework>netcoreapp2.1</TargetFramework>
      <RuntimeIdentifier>win-x86</RuntimeIdentifier>
    </PropertyGroup>
    
  2. 從命令殼層中使用 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/{目標 FRAMEWORK}/{執行階段識別碼}/publish 目錄的內容移至 App Service 中的網站。Move the contents of the bin/Release/{TARGET FRAMEWORK}/{RUNTIME IDENTIFIER}/publish directory to the site in App Service.

將包含 Web 應用程式的 Docker 用於容器Use Docker with Web Apps for containers

Docker Hub 包含最新的預覽 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 App ServiceFor 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 App Service 使用 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: