托管和部署 ASP.NET CoreHost and deploy ASP.NET Core

一般而言,向托管环境部署 ASP.NET Core 应用需执行以下操作:In general, to deploy an ASP.NET Core app to a hosting environment:

  • 将已发布应用部署到托管服务器上的文件夹。Deploy the published app to a folder on the hosting server.
  • 设置进程管理器,该管理器在收到请求时启动应用,并在应用发生故障或服务器重启后重新启动应用。Set up a process manager that starts the app when requests arrive and restarts the app after it crashes or the server reboots.
  • 对于反向代理配置,将反向代理设置为将请求转发到应用。For configuration of a reverse proxy, set up a reverse proxy to forward requests to the app.

发布到文件夹Publish to a folder

dotnet publish 命令编译应用代码,并复制在“发布”文件夹中运行应用所需的文件。The dotnet publish command compiles app code and copies the files required to run the app into a publish folder. 使用 Visual Studio 进行部署时,自动先执行 dotnet publish 步骤,再将文件复制到部署目标。When deploying from Visual Studio, the dotnet publish step occurs automatically before the files are copied to the deployment destination.

文件夹内容Folder contents

“发布”文件夹包含一个或多个应用程序集文件、依赖项以及(可选).NET 运行时。The publish folder contains one or more app assembly files, dependencies, and optionally the .NET runtime.

.NET Core 应用可以发布为“独立式部署”,也可以发布为“依赖框架的部署”。A .NET Core app can be published as self-contained deployment or framework-dependent deployment. 如果应用是独立式,“发布”文件夹中有包含 .NET 运行时的程序集文件。If the app is self-contained, the assembly files that contain the .NET runtime are included in the publish folder. 如果应用依赖于框架,.NET 运行时文件就不包含在内,因为应用包含对服务器上安装的 .NET 版本的引用。If the app is framework-dependent, the .NET runtime files aren't included because the app has a reference to a version of .NET that's installed on the server. 默认部署模型是依赖于框架的模型。The default deployment model is framework-dependent. 有关详细信息,请参阅 .NET Core 应用程序部署For more information, see .NET Core application deployment.

除了“.exe”和“.dll”文件以外,ASP.NET Core 应用的“发布”文件夹通常包含配置文件、静态资产和 MVC 视图。In addition to .exe and .dll files, the publish folder for an ASP.NET Core app typically contains configuration files, static assets, and MVC views. 有关更多信息,请参见ASP.NET Core 目录结构For more information, see ASP.NET Core 目录结构.

设置进程管理器Set up a process manager

ASP.NET Core 应用是一个控制台应用,在服务器启动时必须启动该应用,并且在出现故障后必须重新启动它。An ASP.NET Core app is a console app that must be started when a server boots and restarted if it crashes. 若要自动启动和重新启动,需要使用进程管理器。To automate starts and restarts, a process manager is required. 用于 ASP.NET Core 的最常见进程管理器是:The most common process managers for ASP.NET Core are:

设置反向代理Set up a reverse proxy

如果应用使用 Kestrel 服务器,NginxApacheIIS 可用作反向代理服务器。If the app uses the Kestrel server, Nginx, Apache, or IIS can be used as a reverse proxy server. 反向代理服务器接收来自 Internet 的 HTTP 请求,并将这些请求转发到 Kestrel。A reverse proxy server receives HTTP requests from the Internet and forwards them to Kestrel.

无论配置是否使用反向代理服务器,都是受支持的托管配置。Either configuration—with or without a reverse proxy server—is a supported hosting configuration. 有关详细信息,请参阅何时结合使用 Kestrel 和反向代理For more information, see When to use Kestrel with a reverse proxy.

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

对于托管在代理服务器和负载均衡器后方的应用,可能需要附加配置。Additional configuration might be required for apps hosted behind proxy servers and load balancers. 如不提供附加配置,应用可能无法访问方案 (HTTP/HTTPS) 和发出请求的远程 IP 地址。Without additional configuration, an app might not have access to the scheme (HTTP/HTTPS) and the remote IP address where a request originated. 有关详细信息,请参阅配置 ASP.NET Core 以使用代理服务器和负载均衡器For more information, see Configure ASP.NET Core to work with proxy servers and load balancers.

使用 Visual Studio 和 MSBuild 自动执行部署Use Visual Studio and MSBuild to automate deployments

除将输出从 dotnet publish 复制到服务器外,部署通常还需要其他任务。Deployment often requires additional tasks besides copying the output from dotnet publish to a server. 例如,可能需要从“发布”文件夹获取或排除额外文件。For example, extra files might be required or excluded from the publish folder. Visual Studio 使用 MSBuild 进行 Web 部署,用户可以自定义 MSBuild 以在部署期间执行其他任务。Visual Studio uses MSBuild for web deployment, and MSBuild can be customized to do many other tasks during deployment. 有关详细信息,请参阅 用于 ASP.NET Core 应用部署的 Visual Studio 发布配置文件使用 MSBuild 和 Team Foundation Build 一书。For more information, see 用于 ASP.NET Core 应用部署的 Visual Studio 发布配置文件 and the Using MSBuild and Team Foundation Build book.

通过发布 Web 功能内置 Git 支持,可以将应用从 Visual Studio 直接部署到 Azure 应用服务。By using the Publish Web feature or built-in Git support, apps can be deployed directly from Visual Studio to the Azure App Service. Azure DevOps Services 支持对 Azure 应用服务进行持续部署Azure DevOps Services supports continuous deployment to Azure App Service. 有关详细信息,请参阅通过 ASP.NET Core 和 Azure 实现 DevOpsFor more information, see DevOps with ASP.NET Core and Azure.

发布到 AzurePublish to Azure

有关如何使用 Visual Studio 将应用发布到 Azure 的说明,请参阅 使用 Visual Studio 将 ASP.NET Core 应用发布到 AzureSee 使用 Visual Studio 将 ASP.NET Core 应用发布到 Azure for instructions on how to publish an app to Azure using Visual Studio. 在 Azure 中创建 ASP.NET Core Web 应用提供了其他示例。An additional example is provided by Create an ASP.NET Core web app in Azure.

在 Windows 中使用 MSDeploy 发布Publish with MSDeploy on Windows

请参阅 用于 ASP.NET Core 应用部署的 Visual Studio 发布配置文件,了解如何使用 Visual Studio 发布配置文件发布应用,其中包括在 Windows 命令提示符处使用 dotnet msbuild 命令。See 用于 ASP.NET Core 应用部署的 Visual Studio 发布配置文件 for instructions on how to publish an app with a Visual Studio publish profile, including from a Windows command prompt using the dotnet msbuild command.

在 Web 场中托管Host in a web farm

有关如何配置在 Web 场环境中托管 ASP.NET Core 应用(例如,部署应用的多个实例以实现可伸缩性)的信息,请参阅 在 Web 场中托管 ASP.NET CoreFor information on configuration for hosting ASP.NET Core apps in a web farm environment (for example, deployment of multiple instances of your app for scalability), see 在 Web 场中托管 ASP.NET Core.

执行运行状况检查Perform health checks

使用运行状况检查中间件,对应用及其依赖项执行运行状况检查。Use Health Check Middleware to perform health checks on an app and its dependencies. 有关更多信息,请参见ASP.NET Core 中的运行状况检查For more information, see ASP.NET Core 中的运行状况检查.

其他资源Additional resources