远程调试远程 IIS 计算机上的 ASP.NETRemote Debug ASP.NET on a Remote IIS Computer

若要调试已部署到 IIS 的 ASP.NET 应用程序,请在部署了应用的计算机上安装并运行远程工具,然后从 Visual Studio 附加到正在运行的应用。To debug an ASP.NET application that has been deployed to IIS, install and run the remote tools on the computer where you deployed your app, and then attach to your running app from Visual Studio.

远程调试器组件Remote debugger components

本指南说明如何设置和配置 Visual Studio ASP.NET MVC 4.5.2 应用程序,将其部署到 IIS,并从 Visual Studio 附加远程调试器。This guide explains how to set up and configure a Visual Studio ASP.NET MVC 4.5.2 application, deploy it to IIS, and attach the remote debugger from Visual Studio.

备注

若要改为远程调试 ASP.NET Core,请参阅远程调试 IIS 计算机上的 ASP.NET CoreTo remote debug ASP.NET Core instead, see Remote Debug ASP.NET Core on an IIS Computer. 对于 Azure 应用服务,可以使用 Snapshot Debugger(需要 .NET 4.6.1)或通过从服务器资源管理器附加调试器,在预先配置的 IIS 实例上轻松部署和调试。For Azure App Service, you can easily deploy and debug on a preconfigured instance of IIS using either the Snapshot Debugger (.NET 4.6.1 required) or by attaching the debugger from Server Explorer.

先决条件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.

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

  • Windows Server 2012 R2 和 IIS 8(对于 Windows Server 2008 R2,服务器步骤有所不同)Windows Server 2012 R2 and IIS 8 (For Windows Server 2008 R2, the server steps are different)

网络要求Network requirements

从 Windows Server 2008 Service Pack 2 开始,Windows Server 支持远程调试器。The remote debugger is supported on Windows Server starting with Windows Server 2008 Service Pack 2. 有关要求的完整列表,请参阅要求For a complete list of requirements, see 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.

应用已在 IIS 中运行?App already running in IIS?

本文包含有关在 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 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.

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

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

    在 Visual Studio 2019 中,键入 Ctrl+Q 打开搜索框,键入“asp.net”,选择“模板”,然后选择“创建新的 ASP.NET Web 应用程序(.NET Framework)” 。In Visual Studio 2019, type Ctrl + Q to open the search box, type asp.net, choose Templates, then choose Create new ASP.NET Web Application (.NET Framework). 在出现的对话框中,将项目命名为“MyASPApp”,然后选择“创建” 。In the dialog box that appears, name the project MyASPApp, and then choose Create. 选择“MVC”,并选择“创建” 。Select MVC and choose Create.

    若要在 Visual Studio 2017 中执行此操作,请选择“文件”>“新建”>“项目”,然后选择“Visual C#”>“Web”>“ASP.NET Web 应用程序”。To do this in Visual Studio 2017, choose File > New > Project, then select Visual C# > Web > ASP.NET Web Application. ASP.NET 4.5.2 模板部分中,选择“MVC” 。In the ASP.NET 4.5.2 templates section, select MVC. 请确保未选择“启用 Docker 支持”,并且“身份验证”设置为“无身份验证”。Make sure that Enable Docker Support is not selected and that Authentication is set to No Authentication. 将项目命名为 MyASPApp。)Name the project MyASPApp.)

  2. 打开 HomeController.cs 文件,并在 About() 方法中设置断点。Open the HomeController.cs file, and set a breakpoint in the About() method.

在 Windows Server 上安装和配置 IISInstall and Configure IIS on Windows Server

以下步骤仅显示 IIS 的基本配置。These steps show only a basic configuration of IIS. 有关更深入的信息或要安装到 Windows 桌面计算机,请参阅发布到 IIS使用 ASP.NET 3.5 和 ASP.NET 4.5 的 IIS 8.0For more in-depth information or to install to a Windows Desktop machine, see Publishing to IIS or IIS 8.0 Using ASP.NET 3.5 and ASP.NET 4.5.

对于 Windows Server 操作系统,通过“管理”链接或“服务器管理器”中的“仪表板”链接使用“添加角色和功能”向导 。For Windows Server operating systems, use the Add Roles and Features wizard via the Manage link or the Dashboard link in Server Manager. 在“服务器角色”步骤中,选中“Web 服务器(IIS)”框 。On the Server Roles step, check the box for Web Server (IIS).

在选择服务器角色步骤中选择了“Web 服务器 IIS”角色。

在“角色服务”步骤中,选择所需 IIS 角色服务,或接受提供的默认角色服务 。On the Role services step, select the IIS role services you desire or accept the default role services provided. 如果要使用发布设置和 Web 部署启用部署,请确保选中“IIS 管理脚本和工具” 。If you want to enable deployment using publish settings and Web Deploy, make sure that IIS Management Scripts and Tools is selected.

继续执行确认步骤,安装 Web 服务器角色和服务。Proceed through the confirmation steps to install the web server role and services. 安装 Web 服务器 (IIS) 角色后无需重启服务器/IIS。A server/IIS restart is not required after installing the Web Server (IIS) role.

更新 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

下载软件时,可能会收到授权加载各种网站脚本和资源的请求。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 4.5Install ASP.NET 4.5 on Windows Server

如果需要在 IIS 上安装 ASP.NET 的详细信息,请参阅使用 ASP.NET 3.5 和 ASP.NET 4.5 的 IIS 8.0If you want more detailed information to install ASP.NET on IIS, see IIS 8.0 Using ASP.NET 3.5 and ASP.NET 4.5.

  1. 在服务器管理器的左窗格中,选择“IIS”。In the left pane of Server Manager, select IIS. 右键单击服务器并选择“Internet Information Services (IIS)管理器”。Right-click the server and select Internet Information Services (IIS) Manager.

  2. 使用 Web 平台安装程序 (WebPI) 安装 ASP.NET 4.5(从 Windows Server 2012 R2 的服务器节点,选择“获取新 Web 平台组件”,然后搜索 ASP.NET)Use the Web Platform Installer (WebPI) to install ASP.NET 4.5 (from the Server node in Windows Server 2012 R2, choose Get New Web Platform Components and then search for ASP.NET)

    RemoteDBG_IIS_AspNet_45RemoteDBG_IIS_AspNet_45

    备注

    如果使用 Windows Server 2008 R2,请改为使用以下命令安装 ASP.NET 4:If you are using Windows Server 2008 R2, install ASP.NET 4 instead using this command:

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -irC:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -ir

  3. 重启系统(或在命令提示符处依次执行“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.

备注

此部署方法使用 Web 部署。This deployment method uses Web Deploy. 如果要在 Visual Studio 中手动配置 Web 部署,而不是导入设置,则可以安装 Web 部署 3.6,而不是用于宿主服务器的 Web 部署 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 网站)。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).

在 Windows Server 上安装和配置用于宿主服务器的 Web 部署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.

      备注

      如果要发布到在 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.

  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.

    备注

    如果选择发布配置,则在发布时,需要在 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

如果要使用 PowerShell、RoboCopy 将应用复制到 IIS,或者要手动复制文件,则可以使用此选项来部署应用。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

  1. 打开 Windows 资源管理器并创建新文件夹 C:\Publish,以后将在其中部署 ASP.NET 项目。Open Windows Explorer and create a new folder, C:\Publish, where you will later deploy the ASP.NET project.

  2. 如果尚未打开,请打开“Internet Information Services (IIS) 管理器”。If it's not already open, open the Internet Information Services (IIS) Manager. (在服务器管理器的左窗格中,选择“IIS”。(In the left pane of Server Manager, select IIS. 右键单击服务器并选择“Internet Information Services (IIS)管理器”。)Right-click the server and select Internet Information Services (IIS) Manager.)

  3. 在左窗格中的“连接”下,转到“站点”。Under Connections in the left pane, go to Sites.

  4. 选择“默认网站”,选择“基本设置”,然后将“物理路径”设置为“C:\Publish”。Select the Default Web Site, choose Basic Settings, and set the Physical path to C:\Publish.

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

  6. 将“别名”字段设置为“MyASPApp”,接受默认应用程序池 (DefaultAppPool),并将“物理路径”设置“C:\Publish”。Set the Alias field to MyASPApp, accept the default Application Pool (DefaultAppPool), and set the Physical path to C:\Publish.

  7. 在“连接”下,选择“应用程序池”。Under Connections, select Application Pools. 打开 DefaultAppPool 并将“应用程序池”字段设置为“ASP.NET v4.0”(ASP.NET 4.5 不是应用程序池的选项)。Open DefaultAppPool and set the Application pool field to ASP.NET v4.0 (ASP.NET 4.5 is not an option for the Application pool).

  8. 在 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 none of these users are present, add IUSR as a user with Read & Execute rights.

通过从 Visual Studio 发布到本地文件夹来发布和部署应用Publish and Deploy the app by publishing to a local folder from Visual Studio

还可以使用文件系统或其他工具发布和部署应用。You can also publish and deploy the app using the file system or other tools.

  1. (ASP.NET 4.5.2) 确保 web.config 文件列出了 .NET 的正确版本。(ASP.NET 4.5.2) Make sure that the web.config file lists the correct version of .NET. 例如,如果面向 ASP.NET 4.5.2,请确保此版本在 web.config 中列出。For example, if you are targeting ASP.NET 4.5.2, make sure this version is listed in web.config.

    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
      <httpModules>
        <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
      </httpModules>
    </system.web>
    
    

    例如,如果安装 ASP.NET 4 而不是 4.5.2,则版本应为 4.0。For example, the version should be 4.0 if you install ASP.NET 4 instead of 4.5.2.

  1. 在“解决方案资源管理器” 中,右键单击项目节点并选择“发布” (对于 Web Forms,选择“发布 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:\Publish” 。In the Publish dialog box, select Folder, click Browse, and create a new folder, C:\Publish.

    RemoteDBG_Publish_LocalRemoteDBG_Publish_Local

    对于 Web Forms 应用,请在“发布”对话框中选择“自定义” ,输入配置文件名称,然后选择“确定” 。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.

    备注

    如果使用发布生成,则在发布时,需要在 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 计算机复制到 Windows Server 计算机上为 ASP.NET 应用配置的本地目录中(本例中为 C:\Publish )。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.

    注意

    如果需要更改代码或重新生成,则必须重新发布并重复此步骤。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. 如果没有这样做,则在尝试调试进程时,你会在 Visual Studio 中收到 cannot find or open the PDB file 警告。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 Server 上,通过在浏览器中打开应用来验证是否可以正常运行应用。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. 下载与设备操作系统(x86、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. 下载与设备操作系统(x86、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. 下载与设备操作系统(x86、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 文档For 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.

备注

  • 若要在 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.

    (../media/remotedebuggerwindow.png "远程调试器窗口![远程调试器窗口]")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

备注

如果需要为其他用户添加权限,请更改远程调试器的身份验证模式或端口号,请参阅配置远程调试器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.

有关将远程调试器作为服务运行的信息,请参阅将远程调试器作为服务运行For information on running the remote debugger as a service, see Run the remote debugger as a service.

从 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).

    提示

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

  3. 将限定符字段设置为“<remote computer name>”并按 Enter 。Set the Qualifier field to <remote computer name> and press Enter.

    确保 Visual Studio 将所需的端口添加到计算机名称中,其格式为:<remote computer name>:portVerify that Visual Studio adds the required port to the computer name, which appears in the format: <remote computer name>:port

    在 Visual Studio 2019 上,应看到 <remote computer name>:4024On Visual Studio 2019, you should see <remote computer name>:4024

    在 Visual Studio 2017 上,应看到 <remote computer name>: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.

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

  6. 键入进程名称的第一个字母,以快速查找 w3wp.exe(适用于 ASP.NET 4.5)。Type the first letter of a process name to quickly find w3wp.exe for ASP.NET 4.5.

    如果有多个显示 w3wp.exe 的进程,请检查“用户名”列。If you have multiple processes showing w3wp.exe, check the User Name column. 在某些情况下,“用户名”列显示应用池名称,例如 IIS APPPOOL\DefaultAppPool 。In 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.

    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, you may need to verify that ports are open.

备注

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

必需端口:Required ports:

  • 80 - 对于 IIS 是必需的80 - 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.
  1. 若要在 Windows Server 上打开端口,请打开“开始”菜单,搜索“高级安全 Windows 防火墙”。To open a port on Windows Server, open the Start menu, search for Windows Firewall with Advanced Security.

  2. 然后选择“入站规则”>“新建规则”>“端口”。Then choose Inbound Rules > New Rule > Port. 选择“下一步”,在“特定本地端口”下,输入端口号,单击“下一步”,然后单击“允许连接”,单击“下一步”,然后为入站规则添加名称(“IIS”、“Web 部署”或“msvsmon”)。Choose Next and under Specific local ports, enter the port number, click Next, then Allow the Connection, click Next, and add the name (IIS, Web Deploy, or msvsmon) for the Inbound Rule.

    如果需要有关配置 Windows 防火墙的详细信息,请参阅配置 Windows 防火墙以便进行远程调试If you want more details on configuring Windows Firewall, see Configure the Windows Firewall for Remote Debugging.

  3. 为其他必需端口创建附加规则。Create additional rules for the other required ports.