在 Visual Studio 中的 Azure 中的 IIS 上的远程调试 ASP.NET CoreRemote Debug ASP.NET Core on IIS in Azure in Visual Studio

本指南介绍如何设置和配置 Visual Studio ASP.NET Core 应用,将其部署到 IIS,使用 Azure,并从 Visual Studio 附加远程调试器。This guide explains how to set up and configure a Visual Studio ASP.NET Core app, deploy it to IIS using Azure, and attach the remote debugger from Visual Studio.

在 Azure 上的远程调试的推荐的方式取决于你的方案:The recommended way to remote debug on Azure depends on your scenario:

  • 若要调试在 Azure App Service 上的 ASP.NET Core,请参阅调试 Azure 应用程序使用快照调试器To debug ASP.NET Core on Azure App Service, see Debug Azure apps using the Snapshot Debugger. 这是建议的方法。This is the recommended method.

  • 若要调试 ASP.NET Core 上使用更加传统的调试功能的 Azure App Service,请按照本主题中的步骤 (请参阅明在 Azure App Service 上进行远程调试)。To debug ASP.NET Core on Azure App Service using more traditional debugging features, follow steps in this topic (see the section Remote debug on Azure App Service).

    在此方案中,必须将部署到 Azure 将应用程序从 Visual Studio,但不是需要手动安装或配置 IIS 或远程调试器 (这些组件使用虚线表示),如下图中所示。In this scenario, you must deploy your app from Visual Studio to Azure but you do not need to manually install or configure IIS or the remote debugger (these components are represented with dotted lines), as shown in the following illustration.

    远程调试器组件Remote debugger components

  • 若要调试在 Azure VM 上的 IIS,请按照本主题中的步骤 (请参阅的部分在 Azure VM 上进行远程调试)。To debug IIS on an Azure VM, follow steps in this topic (see the section Remote Debug on an Azure VM). 这允许您使用自定义的配置的 IIS,但更复杂的安装和部署步骤。This allows you to use a customized configuration of IIS, but the setup and deployment steps are more complicated.

    为 Azure VM,必须部署到 Azure 将应用程序从 Visual Studio,您还需要手动安装 IIS 角色和远程调试器下, 图中所示。For an Azure VM, you must deploy your app from Visual Studio to Azure and you also need to manually install the IIS role and the remote debugger, as shown in the following illustration.

    远程调试器组件Remote debugger components

  • 若要调试在 Azure Service Fabric 上的 ASP.NET Core,请参阅调试远程 Service Fabric 应用程序To debug ASP.NET Core on Azure Service Fabric, see Debug a remote Service Fabric application.

Warning

请务必删除时的步骤完成此教程中创建的 Azure 资源。Be sure to delete the Azure resources that you create when you have completed the steps in this tutorial. 这样可以避免产生不必要的费用。That way you can avoid incurring unnecessary charges.

系统必备Prerequisites

Visual Studio 2019 需按照本文中所示的步骤。Visual Studio 2019 is required to follow the steps shown in this article.

Visual Studio 2017 需要按照本文中所示的步骤。Visual Studio 2017 is required to follow the steps shown in this article.

网络要求Network requirements

不支持调试通过代理连接的两台计算机之间。Debugging between two computers connected through a proxy is not supported. 调试通过高延迟或低带宽连接,例如拨号 Internet,或通过 Internet 跨国家/地区不建议并可能会失败或很令人无法接受慢。Debugging over a high latency or low bandwidth connection, such as dialup Internet, or over the Internet across countries is not recommended and may fail or be unacceptably slow. 有关要求的完整列表,请参阅要求For a complete list of requirements, see Requirements.

在 Visual Studio 计算机上创建 ASP.NET Core 应用程序Create the ASP.NET Core application on the Visual Studio computer

  1. 创建新的 ASP.NET Core 应用程序。Create a new ASP.NET Core application.

    在 Visual Studio 2019,键入Ctrl + Q若要打开搜索框中,键入asp.net,选择模板,然后选择创建新的 ASP.NET Core Web 应用程序.In Visual Studio 2019, type Ctrl + Q to open the search box, type asp.net, choose Templates, then choose Create new ASP.NET Core Web Application. 在显示对话框中,该项目命名MyASPApp,然后选择创建In the dialog box that appears, name the project MyASPApp, and then choose Create. 接下来,选择Web 应用程序 (模型-视图-控制器) ,然后选择创建Next, choose Web Application (Model-View-Controller), and then choose Create.

    在 Visual Studio 2017 中,选择文件 > 新建 > 项目,然后选择Visual C# > Web > ASP.NET Core Web 应用程序In Visual Studio 2017, choose File > New > Project, then select Visual C# > Web > ASP.NET Core Web Application. 在 ASP.NET Core 模板部分中,选择Web 应用程序 (模型-视图-控制器)In the ASP.NET Core templates section, select Web Application (Model-View-Controller). 请确保选择了 ASP.NET Core 2.1,该启用 Docker 支持未选中并且身份验证设置为无身份验证Make sure that ASP.NET Core 2.1 is selected, that Enable Docker Support is not selected and that Authentication is set to No Authentication. 将项目命名MyASPAppName the project MyASPApp.

  2. 打开 About.cshtml.cs 文件,并在中设置断点OnGet方法 (在较旧模板中,打开 HomeController.cs 而是和中设置断点About()方法)。Open the About.cshtml.cs file and set a breakpoint in the OnGet method (in older templates, open HomeController.cs instead and set the breakpoint in the About() method).

在 Azure App Service 上的远程调试 ASP.NET CoreRemote Debug ASP.NET Core on an Azure App Service

从 Visual Studio 中,可快速发布和调试你的应用的 IIS 完全预配实例。From Visual Studio, you can quickly publish and debug your app to a fully provisioned instance of IIS. 但是,预设的 IIS 配置,并且不能自定义它。However, the configuration of IIS is preset and you cannot customize it. 有关详细说明,请参阅ASP.NET Core web 应用部署到 Azure 中使用 Visual StudioFor more detailed instructions, see Deploy an ASP.NET Core web app to Azure using Visual Studio. (如果需要自定义 IIS 的功能,请尝试调试Azure VM。)(If you need the ability to customize IIS, try debugging on an Azure VM.)

若要部署的应用程序和使用服务器资源管理器进行远程调试To deploy the app and remote debug using Server Explorer

  1. 在 Visual Studio 中,右键单击项目节点并选择发布In Visual Studio, right-click the project node and choose Publish.

    如果先前配置了任何发布配置文件,则“发布”窗格会显示 。If you have previously configured any publishing profiles, the Publish pane appears. 单击新的配置文件Click New profile.

  2. 选择Azure 应用服务发布对话框中,选择新建,并按照提示来发布。Choose Azure App Service from the Publish dialog box, select Create New, and follow the prompts to publish.

    有关详细说明,请参阅使用 Visual Studio 将 ASP.NET Core Web 应用部署到 AzureFor detailed instructions, see Deploy an ASP.NET Core web app to Azure using Visual Studio.

    发布到 Azure 应用服务

  3. 打开服务器资源管理器(视图 > 服务器资源管理器),应用服务实例上右键单击并选择附加调试器.Open Server Explorer (View > Server Explorer), right-click on the App Service instance and choose Attach Debugger.

  4. 在运行的 ASP.NET 应用程序,单击链接到有关页。In the running ASP.NET application, click the link to the About page.

    应在 Visual Studio 中命中断点。The breakpoint should be hit in Visual Studio.

    就这么简单!That's it! 本主题中的步骤的其余部分适用于 Azure VM 上远程调试。The rest of the steps in this topic apply to remote debugging on an Azure VM.

在 Azure VM 上的远程调试 ASP.NET CoreRemote Debug ASP.NET Core on an Azure VM

可以创建服务器的 Azure VM 的 Windows,然后安装和配置 IIS 和其他所需的软件组件。You can create an Azure VM for Windows Server and then install and configure IIS and the other required software components. 这要花更多的时间比部署到 Azure 应用服务,要求你按照本教程中的剩余步骤。This takes more time than deploying to an Azure App Service and requires that you follow the remaining steps in this tutorial.

这些服务器配置上进行了测试这些过程:These procedures have been tested on these server configurations:

  • Windows Server 2012 R2 和 IIS 8Windows Server 2012 R2 and IIS 8
  • Windows Server 2016 和 IIS 10Windows Server 2016 and IIS 10

应用程序已在 IIS 中运行 Azure VM 上吗?App already running in IIS on the Azure VM?

本文包含有关 Windows server 上的 IIS 的基本配置设置和部署该应用程序从 Visual Studio 的步骤。This article includes steps on setting up a basic configuration of IIS on Windows server and deploying the app from Visual Studio. 这些步骤是包括在内,以确保服务器具有所需的组件安装,可以正确,运行该应用程序,您就为远程调试。These steps are included to make sure that the server has the required components installed, that the app can run correctly, and that you are ready to remote debug.

  • 如果在 IIS 中运行您的应用程序,并且只是想要下载远程调试器和启动调试,请转到下载并安装 Windows Server 上的远程工具If your app is running in IIS and you just want to download the remote debugger and start debugging, go to Download and Install the remote tools on Windows Server.

  • 如果需要帮助,确保您的应用程序设置已完成,部署,并正确运行在 IIS 中,以便可以调试,请按照本主题中的所有步骤。If you want help to make sure that your app is set up, deployed, and running correctly in IIS so that you can debug, follow all the steps in this topic.

    • 在开始之前,请按照中所述的所有步骤安装和运行的 IISBefore you begin, follow all the steps described in Install and run IIS.

    • 当网络安全组中打开端口 80 时,打开更正端口远程调试器 (4024 或 4022)。When you open port 80 in the Network security group, also open the correct port for the remote debugger (4024 or 4022). 这样一来,无需以后将其打开。That way, you won't have to open it later.

更新 Windows Server 上的浏览器安全设置Update browser security settings on Windows Server

如果在 Internet Explorer (默认情况下已启用) 中启用了增强的安全配置,您可能需要将某些域添加为受信任的站点,以便可以下载某些 web 服务器组件。If Enhanced Security Configuration is enabled in Internet Explorer (it is enabled by default), then you may need to add some domains as trusted sites to enable you to download some of the web server components. 添加可信的站点,通过转到Internet 选项 > 安全性 > 受信任的站点 > 站点Add the trusted sites by going to Internet Options > Security > Trusted Sites > Sites. 添加以下域。Add the following domains.

  • microsoft.commicrosoft.com
  • go.microsoft.comgo.microsoft.com
  • download.microsoft.comdownload.microsoft.com
  • iis.netiis.net

下载软件时,可能会显示请求授予权限以加载各种 web 站点脚本和资源。When you download the software, you may get requests to grant permission to load various web site scripts and resources. 以下一些资源不是必需的但若要简化此过程中,单击添加出现提示时。Some of these resources are not required, but to simplify the process, click Add when prompted.

在 Windows Server 上安装 ASP.NET CoreInstall ASP.NET Core on Windows Server

  1. 在托管系统上安装 .NET Core Windows Server 托管捆绑包Install the .NET Core Windows Server Hosting bundle on the hosting system. 捆绑包将安装.NET Core 运行时,.NET Core 库和 ASP.NET Core 模块。The bundle will install the .NET Core Runtime, .NET Core Library, and the ASP.NET Core Module. 有关更多深入说明,请参阅发布到 IISFor more in-depth instructions, see Publishing to IIS.

    Note

    如果系统没有 Internet 连接,请先获取并安装 Microsoft Visual C++ 2015 Redistributable,再安装 .NET Core Windows Server 托管捆绑包 。If the system doesn't have an Internet connection, obtain and install the Microsoft Visual C++ 2015 Redistributable before installing the .NET Core Windows Server Hosting bundle.

  2. 重启系统(或在命令提示符处依次执行“net stop was /y”和“net start w3svc”,了解系统路径的更改) 。Restart the system (or execute net stop was /y followed by net start w3svc from a command prompt to pick up a change to the system PATH).

选择部署选项Choose a deployment option

如果您需要帮助将应用部署到 IIS,请考虑这些选项:If you need help to deploy the app to IIS, consider these options:

  • 通过在 IIS 中创建的发布设置文件和导入 Visual Studio 中的设置部署。Deploy by creating a publish settings file in IIS and importing the settings in Visual Studio. 在某些情况下,这是一种将应用部署的快速方法。In some scenarios, this is a fast way to deploy your app. 创建发布设置文件时,权限自动将会在 IIS 中设置。When you create the publish settings file, permissions are automatically set up in IIS.

  • 部署发布到本地文件夹并将输出的首选方法复制到 IIS 上的已准备好应用程序文件夹。Deploy by publishing to a local folder and copying the output by a preferred method to a prepared app folder on IIS.

(可选)使用发布设置文件进行部署(Optional) Deploy using a publish settings file

可以使用此选项创建发布设置文件,并将其导入 Visual Studio。You can use this option create a publish settings file and import it into Visual Studio.

Note

此部署方法使用 Web 部署。This deployment method uses Web Deploy. 如果你想要 Web 部署手动配置 Visual Studio 中而不是导入的设置,可以为托管服务器安装 Web 部署而不是 Web 部署 3.6 3.6。If you want to configure Web Deploy manually in Visual Studio instead of importing the settings, you can install Web Deploy 3.6 instead of Web Deploy 3.6 for Hosting Servers. 但是,如果手动配置 Web 部署,您将需要确保在服务器上的应用程序文件夹配置具有正确的值和权限 (请参阅配置 ASP.NET Web 站点)。However, if you configure Web Deploy manually, you will need to make sure that an app folder on the server is configured with the correct values and permissions (see Configure ASP.NET Web site).

安装和配置 Web 部署以便在 Windows Server 上的宿主服务器Install and configure Web Deploy for Hosting Servers on Windows Server

用于托管服务器的 Web 部署 3.6 提供额外的配置功能,可实现从 UI 创建发布设置文件。Web Deploy 3.6 for Hosting Servers provides additional configuration features that enable the creation of the publish settings file from the UI.

  1. 如果在 Windows 服务器上已安装 Web 部署 3.6,请使用“控制面板” > “程序” > “卸载程序”将其卸载。 If you have Web Deploy 3.6 already installed on Windows Server, uninstall it using Control Panel > Programs > Uninstall a Program.

  2. 接下来,在 Windows 服务器上安装用于托管服务器的 Web 部署 3.6。Next, install Web Deploy 3.6 for Hosting Servers on Windows Server.

    要安装用于托管服务器的 Web 部署,请使用 Web 平台安装程序 (WebPI)To install Web Deploy for Hosting Servers, use the Web Platform Installer (WebPI). (要从 IIS 查找 Web 平台安装程序链接,请选择服务器管理器左侧窗格中的“IIS” 。(To find the Web Platform Installer link from IIS, select IIS in the left pane of Server Manager. 右键单击服务器并选择“Internet Information Services (IIS)管理器” 。)Right-click the server and select Internet Information Services (IIS) Manager.)

    在 Web 平台安装程序中,在“应用程序”选项卡中查找“用于托管服务器的 Web 部署” 。In the Web Platform Installer, you find Web Deploy for Hosting Servers in the Applications tab.

  3. 如果尚未安装“IIS 管理脚本和工具”,请立即安装 。If you did not already install IIS Management Scripts and Tools, install it now.

    转到“选择服务器角色” > “Web 服务器(IIS)” > “管理工具”,然后选择“IIS 管理脚本和工具”角色,点击“下一步”,然后安装角色 。Go to Select server roles > Web Server (IIS) > Management Tools, and then select the IIS Management Scripts and Tools role, click Next, and then install the role.

    安装 IIS 管理脚本和工具

    需要脚本和工具来生成发布设置文件。The scripts and tools are required to enable the generation of the publish settings file.

  4. 根据需要,打开“控制面板”>“系统和安全”>“管理工具”>“服务”来验证 Web 部署是否正确运行,并确保“Web 部署代理服务”正在运行(旧版本中的服务名称不同) 。(Optional) Verify that Web Deploy is running correctly by opening Control Panel > System and Security > Administrative Tools > Services and make sure that Web Deployment Agent Service is running (the service name is different in older versions).

    如果代理服务未运行,请启动该服务。If the agent service is not running, start it. 如果服务根本不存在,请转到“控制面板”>“程序”>“卸载程序” ,查找“Microsoft Web 部署 <版本>” 。If it is not present at all, go to Control Panel > Programs > Uninstall a program, find Microsoft Web Deploy <version>. 选择“更改”安装,并确保对 Web 部署组件选择“将安装到本地驱动器” 。Choose to Change the installation and make sure that you choose Will be installed to the local hard drive for the Web Deploy components. 完成更改安装步骤。Complete the change installation steps.

在 Windows Server 上的 IIS 中创建发布设置文件Create the publish settings file in IIS on Windows Server

  1. 关闭并重新打开 IIS 管理控制台以在 UI 中显示更新的配置选项。Close and reopen the IIS Management Console to show updated configuration options in the UI.

  2. 在 IIS 中,右键单击“默认网站”,选择“部署” > “配置 Web 部署发布” 。In IIS, right-click the Default Web Site, choose Deploy > Configure Web Deploy Publishing.

    配置 Web 部署配置

  3. 在“配置 Web 部署发布”对话框中,检查设置 。In the Configure Web Deploy Publishing dialog box, examine the settings.

  4. 单击“设置” 。Click Setup.

    在“结果”面板中,输出显示已为指定用户授予访问权限,并且已在对话框中显示的位置生成了具有 .publishsettings 文件扩展名的文件 。In the Results panel, the output shows that access rights are granted to the specified user, and that a file with a .publishsettings file extension has been generated in the location shown in the dialog box.

    <?xml version="1.0" encoding="utf-8"?>
    <publishData>
      <publishProfile
        publishUrl="https://myhostname:8172/msdeploy.axd"
        msdeploySite="Default Web Site"
        destinationAppUrl="http://myhostname:80/"
        mySQLDBConnectionString=""
        SQLServerDBConnectionString=""
        profileName="Default Settings"
        publishMethod="MSDeploy"
        userName="myhostname\myusername" />
    </publishData>
    

    根据 Windows Server 和 IIS 配置,可以在 XML 文件看到不同的值。Depending on your Windows Server and IIS configuration, you see different values in the XML file. 下面是有关所看到的值的一些详细信息:Here are a few details about the values that you see:

    • publishUrl 属性中引用的 msdeploy.axd 文件是 Web 部署动态生成的 HTTP 处理程序文件 。The msdeploy.axd file referenced in the publishUrl attribute is a dynamically generated HTTP handler file for Web Deploy. (用于测试目的,http://myhostname:8172 通常也适用。)(For testing purposes, http://myhostname:8172 generally works as well.)

    • publishUrl 端口设置为端口 8172,这是 Web 部署的默认端口。The publishUrl port is set to port 8172, which is the default for Web Deploy.

    • destinationAppUrl 端口设置为端口 80,这是 IIS 部署的默认端口。The destinationAppUrl port is set to port 80, which is the default for IIS.

    • 如果使用主机名无法在 Visual Studio 中连接到远程主机(在后续步骤中),请测试 IP 地址以代替主机名。If you are unable to connect to the remote host in Visual Studio using the host name (in later steps), test the IP address in place of the host name.

      Note

      如果要发布到在 Azure VM 上运行的 IIS,则必须在网络安全组中打开 Web 部署和 IIS 端口。If you are publishing to IIS running on an Azure VM, you must open the Web Deploy and IIS ports in the Network Security group. 有关详细信息,请参阅安装和运行 IISFor detailed information, see Install and run IIS.

  5. 将此文件复制到运行 Visual Studio 的计算机上。Copy this file to the computer where you are running Visual Studio.

在 Visual Studio 中导入发布设置并进行部署Import the publish settings in Visual Studio and deploy

  1. 如果计算机上的 Visual Studio 中打开有 ASP.NET 项目,则在该计算机的解决方案资源管理器中右键单击该项目,然后选择“发布” 。On the computer where you have the ASP.NET project open in Visual Studio, right-click the project in Solution Explorer, and choose Publish.

  2. 如果先前配置了任何发布配置文件,则“发布”窗格会显示 。If you have previously configured any publishing profiles, the Publish pane appears. 点击“新建配置文件” 。Click Create new profile.

  3. 在“选取发布目标”对话框中,点击“导入配置文件” 。In the Pick a publish target dialog box, click Import Profile.

    选择发布

  4. 导航到上一节中创建的发布设置文件的位置。Navigate to the location of the publish settings file that you created in the previous section.

  5. 在“导入发布设置文件”对话框中,导航到并选择在上一节中创建的配置文件,然后单击“打开” 。In the Import Publish Settings File dialog box, navigate to and select the profile that you created in the previous section, and click Open.

    Visual Studio 开始执行部署过程,并且输出窗口将显示进度和结果。Visual Studio begins the deployment process, and the Output window shows progress and results.

    如果出现任何部署错误,请单击“设置”以编辑设置 。If you get an any deployment errors, click Settings to edit settings. 修改设置,然后单击“验证”以测试新设置 。Modify settings and click Validate to test new settings. 如果找不到主机名,请尝试“服务器”和“目标 URL”字段中的 IP 地址而不是主机名 。If the host name is not found, try the IP address instead of the host name in the Server and Destination URL fields.

    编辑发布工具中的设置

应用成功部署后,它应自动启动。After the app deploys successfully, it should start automatically. 如果从 Visual Studio 不启动应用程序,请在 IIS 中启动应用程序。If the app does not start from Visual Studio, start the app in IIS. 对于 ASP.NET Core,需要确保将 DefaultAppPool 的应用程序池字段设置为“无托管代码” 。For ASP.NET Core, you need to make sure that the Application pool field for the DefaultAppPool is set to No Managed Code.

  1. 在中设置对话框中,通过单击调试启用下一步,选择调试配置,然后选择删除其他文件目标文件发布选项。In the Settings dialog box, enable debugging by clicking Next, choose a Debug configuration, and then choose Remove additional files at destination under the File Publish options.

    Note

    如果选择发布配置,则禁用在中进行调试web.config文件在发布时。If you choose a Release configuration, you disable debugging in the web.config file when you publish.

  2. 单击保存然后重新发布应用程序。Click Save and then republish the app.

(可选)部署发布到本地文件夹(Optional) Deploy by publishing to a local folder

此选项可用于部署您的应用程序,如果你想要将应用程序复制到 IIS,使用 Powershell,RoboCopy,或者想要手动复制文件。You can use this option to deploy your app if you want to copy the app to IIS using Powershell, RoboCopy, or you want to manually copy the files.

Windows Server 计算机上配置 ASP.NET 网站Configure the ASP.NET Web site on the Windows Server computer

如果要导入发布设置,则可以跳过此部分。If you are importing publish settings, you can skip this section.

  1. 打开“Internet 信息服务(IIS)管理器” 并转到“站点” 。Open the Internet Information Services (IIS) Manager and go to Sites.

  2. 右键单击“默认网站” 节点,然后选择“添加应用程序” 。Right-click the Default Web Site node and select Add Application.

  3. 设置别名字段MyASPApp和应用程序池域无托管代码Set the Alias field to MyASPApp and the Application pool field to No Managed Code. 设置物理路径C:\Publish (其中更高版本部署的 ASP.NET 项目)。Set the Physical path to C:\Publish (where you will later deploy the ASP.NET project).

  4. 在 IIS 管理器中选择站点后,选择编辑权限,并确保该 IUSR、 IIS_IUSRS 或配置应用程序池为授权的用户使用读取和执行权限的用户。With the site selected in the IIS Manager, choose Edit Permissions, and make sure that IUSR, IIS_IUSRS, or the user configured for the Application Pool is an authorized user with Read & Execute rights.

    如果看不到具有访问这些用户,请执行步骤将 IUSR 添加为具有读取和执行权限的用户。If you don't see one of these users with access, go through steps to add IUSR as a user with Read & Execute rights.

(可选)发布和部署应用程序通过发布到本地文件夹从 Visual Studio(Optional) Publish and Deploy the app by publishing to a local folder from Visual Studio

如果不使用 Web 部署,必须发布和部署应用程序使用文件系统或其他工具。If you're not using Web Deploy, you must publish and deploy the app using the file system or other tools. 您可以首先使用文件系统中,创建包,然后手动部署包或使用其他工具,例如 PowerShell、 RoboCopy 或 XCopy。You can start by creating a package using the file system, and then either deploy the package manually or use other tools like PowerShell, RoboCopy, or XCopy. 在本部分中,我们假设要手动将复制包,如果不使用 Web 部署。In this section, we assume you are manually copying the package if you are not using Web Deploy.

  1. 在中解决方案资源管理器,右键单击项目节点并选择发布(Web 窗体发布 Web 应用)。In the Solution Explorer, right-click the project node and select Publish (for Web Forms, Publish Web App).

    如果先前配置了任何发布配置文件,则“发布”窗格会显示 。If you have previously configured any publishing profiles, the Publish pane appears. 单击新的配置文件Click New profile.

  2. 在中发布对话框中,选择文件夹,单击浏览,并创建一个新文件夹C:\PublishIn the Publish dialog box, select Folder, click Browse, and create a new folder, C:\Publish.

    RemoteDBG_Publish_LocalRemoteDBG_Publish_Local

    对于 Web 窗体应用中,选择自定义在发布对话框中,输入配置文件名称,然后选择确定For a Web Forms app, choose Custom in the Publish dialog box, enter a profile name, and choose OK.

  3. 单击创建配置文件下拉列表中 (发布是默认值)。Click Create profile in the drop-down list (Publish is the default value).

  4. 在中发布对话框中,单击设置链接,然后再选择设置选项卡。In the Publish dialog box, click the Settings link, and then select the Settings tab.

  5. 将配置设置为调试,选择删除所有现有的文件发布前,然后单击保存Set the configuration to Debug, select Delete all existing files prior to publish, and then click Save.

    Note

    如果使用发布版本,则禁用调试的 web.config 文件中,当你发布时。If you use a Release build, you disable debugging in the web.config file when you publish.

  6. 单击“发布” 。Click Publish.

    RemoteDBG_Publish_Debug_ConfigRemoteDBG_Publish_Debug_Config

    应用程序将发布调试到本地文件夹的项目配置。The application publishes a Debug configuration of the project to the local folder. 在输出窗口中显示进度。Progress shows in the Output window.

  7. 将 ASP.NET 项目目录从 Visual Studio 计算机复制到配置为 ASP.NET 应用程序的本地目录 (在此示例中, C:\Publish) Windows Server 计算机上。Copy the ASP.NET project directory from the Visual Studio computer to the local directory configured for the ASP.NET app (in this example, C:\Publish) on the Windows Server computer. 在本教程中,我们假定您要手动复制,但可以使用 PowerShell、 Xcopy 或 Robocopy 等其他工具。In this tutorial, we assume you are copying manually, but you can use other tools like PowerShell, Xcopy, or Robocopy.

    Caution

    如果需要更改代码或重新生成,必须重新发布并重复此步骤。If you need to make changes to the code or rebuild, you must republish and repeat this step. 复制到远程计算机的可执行文件必须与你的本地源和符号完全匹配。The executable you copied to the remote machine must exactly match your local source and symbols. 如果您不这样做将收到cannot find or open the PDB file警告在 Visual Studio 中,当您尝试调试进程时。If you do not do this you will receive a cannot find or open the PDB file warning in Visual Studio when you attempt to debug the process.

  8. 在 Windows 服务器上,验证可以正确运行应用,通过在浏览器中打开应用。On the Windows Server, verify that you can run the app correctly by opening the app in your browser.

    如果应用程序无法正常运行,在你的服务器和在 Visual Studio 计算机上安装 ASP.NET 的版本之间可能存在不匹配或可能使用 IIS 或网站配置存在问题。If the app doesn't run correctly, there may be a mismatch between the version of ASP.NET installed on your server and your Visual Studio machine, or you may have an issue with your IIS or Web site configuration. 重新检查前面的步骤。Recheck earlier steps.

下载并安装 Windows Server 上的远程工具Download and Install the remote tools on Windows Server

下载与你的 Visual Studio 版本匹配远程工具的版本。Download the version of the remote tools that matches your version of Visual Studio.

在远程设备或服务器,你想要调试,而非 Visual Studio 计算机,下载并安装远程工具的正确版本从下表中的链接。On the remote device or server that you want to debug on, rather than the Visual Studio machine, download and install the correct version of the remote tools from the links in the following table.

  • 下载最新版本的 Visual Studio 远程工具。Download the most recent remote tools for your version of Visual Studio. 最新的远程工具版本兼容使用早期的 Visual Studio 版本,但早期的远程工具版本不是与更高版本的 Visual Studio 版本兼容。The latest remote tools version is compatible with earlier Visual Studio versions, but earlier remote tools versions aren't compatible with later Visual Studio versions. (例如,如果使用 Visual Studio 2017,下载适用于 Visual Studio 2017 远程工具的最新的更新。(For example, if you are using Visual Studio 2017, download the latest update of the remote tools for Visual Studio 2017. 在此方案中,不要下载远程工具的 Visual Studio 2019。)In this scenario, do not download the remote tools for Visual Studio 2019.)
  • 下载的远程工具使用相同的体系结构在计算机安装它们。Download the remote tools with the same architecture as the machine you're installing them on. 例如,如果你想要调试远程计算机运行 64 位操作系统上的 32 位应用程序,安装 64 位远程工具。For example, if you want to debug a 32-bit app on a remote computer running a 64-bit operating system, install the 64-bit remote tools.
VersionVersion 链接Link 说明Notes
Visual Studio 2019Visual Studio 2019 远程工具Remote tools 与所有 Visual Studio 2019 版本兼容。Compatible with all Visual Studio 2019 versions. 下载匹配您设备的操作系统 (x 86、 x64、 或 ARM64) 的版本。Download the version matching your device operating system (x86, x64, or ARM64). 在 Windows 服务器上,请参阅取消阻止文件下载下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2017Visual Studio 2017 远程工具Remote tools 与所有 Visual Studio 2017 版本兼容。Compatible with all Visual Studio 2017 versions. 下载匹配您设备的操作系统 (x 86、 x64、 或 ARM64) 的版本。Download the version matching your device operating system (x86, x64, or ARM64). 在 Windows 服务器上,请参阅取消阻止文件下载下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2015Visual Studio 2015 远程工具Remote tools Visual Studio 2015 远程工具可从 My.VisualStudio.com。Remote tools for Visual Studio 2015 are available from My.VisualStudio.com. 如果系统提示,请加入免费Visual Studio Dev Essentials程序或使用 Visual Studio 订阅 id。 登录If prompted, join the free Visual Studio Dev Essentials program, or sign in with your Visual Studio subscription ID. 在 Windows 服务器上,请参阅取消阻止文件下载下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2013Visual Studio 2013 远程工具Remote tools 下载 Visual Studio 2013 文档中的页Download page in Visual Studio 2013 documentation
Visual Studio 2012Visual Studio 2012 远程工具Remote tools 下载 Visual Studio 2012 文档中的页Download page in Visual Studio 2012 documentation
VersionVersion 链接Link 说明Notes
Visual Studio 2017Visual Studio 2017 远程工具Remote tools 与所有 Visual Studio 2017 版本兼容。Compatible with all Visual Studio 2017 versions. 下载匹配您设备的操作系统 (x 86、 x64、 或 ARM64) 的版本。Download the version matching your device operating system (x86, x64, or ARM64). 在 Windows 服务器上,请参阅取消阻止文件下载下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools. 有关远程工具的最新版本,打开Visual Studio 2019 docFor the most recent version of the remote tools, open the Visual Studio 2019 doc.
Visual Studio 2015Visual Studio 2015 远程工具Remote tools Visual Studio 2015 远程工具可从 My.VisualStudio.com。Remote tools for Visual Studio 2015 are available from My.VisualStudio.com. 如果系统提示,请加入免费Visual Studio Dev Essentials程序或使用 Visual Studio 订阅 id。 登录If prompted, join the free Visual Studio Dev Essentials program, or sign in with your Visual Studio subscription ID. 在 Windows 服务器上,请参阅取消阻止文件下载下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2013Visual Studio 2013 远程工具Remote tools 下载 Visual Studio 2013 文档中的页Download page in Visual Studio 2013 documentation
Visual Studio 2012Visual Studio 2012 远程工具Remote tools 下载 Visual Studio 2012 文档中的页Download page in Visual Studio 2012 documentation

复制可运行远程调试器msvsmon.exe到远程计算机,而不是安装远程工具。You can run the remote debugger by copying msvsmon.exe to the remote computer, rather than installing the remote tools. 但是,远程调试器配置向导 (rdbgwiz.exe) 可仅当安装远程工具。However, the Remote Debugger Configuration Wizard (rdbgwiz.exe) is available only when you install the remote tools. 您可能需要使用该向导进行配置,如果你想要远程调试器作为服务运行。You may need to use the wizard for configuration if you want to run the remote debugger as a service. 有关详细信息,请参阅(可选) 配置远程调试器作为服务For more information, see (Optional) Configure the remote debugger as a service.

Note

  • 若要调试 ARM 设备上的 Windows 10 应用,使用 ARM64,这是可用的远程工具的最新版本。To debug Windows 10 apps on ARM devices, use ARM64, which is available with the latest version of the remote tools.
  • 若要调试在 Windows RT 设备上的 Windows 10 应用,使用 ARM,仅在 Visual Studio 2015 远程工具下载中可用。To debug Windows 10 apps on Windows RT devices, use ARM, which is available only in the Visual Studio 2015 remote tools download.

设置 Windows Server 上的远程调试器Set up the remote debugger on Windows Server

  1. 在远程计算机上查找和启动远程调试器启动菜单。On the remote computer, find and start the Remote Debugger from the Start menu.

    如果在远程计算机上没有管理权限,请右击远程调试器应用,然后选择以管理员身份运行If you don't have administrative permissions on the remote computer, right-click the Remote Debugger app and select Run as administrator. 否则,只是它正常启动。Otherwise, just start it normally.

    如果想要附加到正在运行以管理员身份,或不同的用户下运行的进程帐户 (如 IIS) 中,右键单击远程调试器应用,然后选择以管理员身份运行.If you are planning to attach to a process which is running as an administrator, or is running under a different user account (such as IIS), right-click the Remote Debugger app and select Run as administrator. 有关详细信息,请参阅以管理员身份运行远程调试器For more information, see Run the remote debugger as an administrator.

  2. 第一次启动远程调试器 (或之前已配置),远程调试配置对话框随即出现。The first time you start the remote debugger (or before you have configured it), the Remote Debugging Configuration dialog box appears.

    远程调试器配置Remote Debugger configuration

  3. 如果 Windows Web 服务 API 未安装,这种情况发生,仅在 Windows Server 2008 R2 上,选择安装按钮。If the Windows Web Services API is not installed, which happens only on Windows Server 2008 R2, select the Install button.

  4. 选择你想要在使用远程工具的至少一个网络类型。Select at least one network type you want to use the remote tools on. 如果这些计算机通过域连接,则必须选择第一项。If the computers are connected through a domain, you must choose the first item. 如果这些计算机通过工作组或家庭组连接,选择相应的第二个或第三个项。If the computers are connected through a workgroup or homegroup, choose the second or third item as appropriate.

  5. 选择配置远程调试,配置防火墙并启动远程调试器。Select Configure remote debugging to configure the firewall and start the remote debugger.

  6. 配置完成后,远程调试器窗口会显示。When configuration is complete, the Remote Debugger window appears.

    远程调试器窗口Remote Debugger window

    远程调试器现在正在等待连接。The remote debugger is now waiting for a connection. 使用服务器名称和端口号显示在 Visual Studio 中设置的远程连接配置。Use the server name and port number shown to set the remote connection configuration in Visual Studio.

若要停止远程调试器,请选择文件 > 退出To stop the remote debugger, select File > Exit. 您可以重新启动它从启动菜单中,或从命令行:You can restart it from the Start menu, or from the command line:

<Remote debugger installation directory>\msvsmon.exe

Note

如果您需要添加其他用户的权限更改身份验证模式,或者远程调试器的端口号,请参阅配置远程调试器If you need to add permissions for additional users, change the authentication mode, or port number for the remote debugger, see Configure the remote debugger.

从 Visual Studio 计算机附加到 ASP.NET 应用程序Attach to the ASP.NET application from the Visual Studio computer

  1. Visual Studio 计算机上,打开要调试的解决方案 (MyASPApp在您按照这篇文章中的步骤)。On the Visual Studio computer, open the solution that you are trying to debug (MyASPApp if you are following the steps in this article).

  2. 在 Visual Studio 中,单击调试 > 附加到进程(Ctrl + Alt + P)。In Visual Studio, click Debug > Attach to Process (Ctrl + Alt + P).

    Tip

    在 Visual Studio 2017 和更高版本中,您可以将重新附加到您以前使用附加到的同一个进程调试 > 重新附加到进程... (Shift + Alt + P)。In Visual Studio 2017 and later versions, you can re-attach to the same process you previously attached to by using Debug > Reattach to Process... (Shift+Alt+P).

  3. 将限定符字段设置为 <远程计算机名称> 然后按EnterSet the Qualifier field to <remote computer name> and press Enter.

    验证 Visual Studio 将所需的端口添加到计算机名称,将出现在格式: <远程计算机名称>:端口Verify that Visual Studio adds the required port to the computer name, which appears in the format: <remote computer name>:port

    在 Visual Studio 2019,你会看到 <远程计算机名称>:4024On Visual Studio 2019, you should see <remote computer name>:4024

    在 Visual Studio 2017 中,你会看到 <远程计算机名称>:4022On Visual Studio 2017, you should see <remote computer name>:4022

    端口是必需的。The port is required. 如果看不到的端口号,请手动添加它。If you don't see the port number, add it manually.

  4. 单击“刷新” 。Click Refresh. “可用进程” 窗口中将显示某些进程。You should see some processes appear in the Available Processes window.

    如果看不到任何进程,请尝试使用的 IP 地址而不远程计算机名称 (端口是必需的)。If you don't see any processes, try using the IP address instead of the remote computer name (the port is required). 可以使用ipconfig获取 IPv4 地址的命令行中。You can use ipconfig in a command line to get the IPv4 address.

    如果你想要使用查找按钮,可能需要向打开 UDP 端口 3702在服务器上。If you want to use the Find button, you may need to open UDP port 3702 on the server.

  5. 勾选“显示所有用户的进程” 。Check Show processes from all users.

  6. 键入您的进程名称以快速查找您的应用程序的第一个字母。Type the first letter of your process name to quickly find your app.

    • 选择dotnet.exe (适用于.NET Core)Select dotnet.exe (for .NET Core)

      如果有多个进程显示dotnet.exe,检查用户名列。If you have multiple processes showing dotnet.exe, check the User Name column. 在某些情况下,用户名列显示你的应用程序池名称,如IIS APPPOOL\DefaultAppPoolIn some scenarios, the User Name column shows your app pool name, such as IIS APPPOOL\DefaultAppPool. 如果你看到应用程序池标识正确的进程的简单办法是创建一个新应用池命名为你想要调试的应用程序实例,然后您可以找到它轻松地在用户名列。If you see the App Pool, an easy way to identify the correct process is to create a new named App Pool for the app instance you want to debug, and then you can find it easily in the User Name column.

    • 在 IIS 某些情况下,您可能会发现你的应用名称在进程列表中,如MyASPApp.exeIn some IIS scenarios, you may find your app name in the process list, such as MyASPApp.exe. 可以改为附加到此进程。You can attach to this process instead.

    RemoteDBG_AttachToProcessRemoteDBG_AttachToProcess

    RemoteDBG_AttachToProcessRemoteDBG_AttachToProcess

  7. 单击 “附加”Click Attach.

  8. 打开远程计算机的网站。Open the remote computer's website. 在浏览器中,转到 http://<remote computer name> 。In a browser, go to http://<remote computer name>.

    将显示 ASP.NET 网页。You should see the ASP.NET web page.

  9. 在运行的 ASP.NET 应用程序,单击链接到有关页。In the running ASP.NET application, click the link to the About page.

    应在 Visual Studio 中命中断点。The breakpoint should be hit in Visual Studio.

故障排除:Windows Server 上打开所需的端口Troubleshooting: Open required ports on Windows Server

在大多数系统中,所需的端口被打开的 ASP.NET 和远程调试器安装。In most setups, required ports are opened by the installation of ASP.NET and the remote debugger. 但是,如果你解决部署问题和应用程序托管在防火墙后面,你可能需要验证正确的端口打开。However, if you are troubleshooting deployment issues and the app is hosted behind a firewall, you may need to verify that the correct ports are open.

在 Azure VM 上,则必须打开端口通过网络安全组On an Azure VM, you must open ports through the Network security group.

所需的端口:Required ports:

  • 80-所需的 IIS80 - Required for IIS
  • UDP 3702-(可选) 发现端口使你能够查找按钮时将附加到 Visual Studio 中的远程调试器。UDP 3702 - (Optional) Discovery port enables you to the Find button when attaching to the remote debugger in Visual Studio.

此外,应已 ASP.NET 安装可打开这些端口:In addition, these ports should already be opened by the ASP.NET installation:

  • 8172-(可选) 所需的 Web 部署来部署该应用程序从 Visual Studio8172 - (Optional) Required for Web Deploy to deploy the app from Visual Studio