我应该将 Visual Studio 调试器附加到哪个进程来调试 Kestrel 应用程序?

To which process should I attach Visual Studio Debugger to debug a Kestrel application?

我正在使用 dotnet run 命令调出命令行和 运行 我的应用程序。这将启动 Kestrel 并启动我的应用程序。

我应该如何确定要将调试器附加到哪个进程,以便我可以调试 Kestrel 现在托管的网站?

我特别需要能够这样做 - 这意味着我不能使用标准 F5。

不幸的是,目前无法使用 Visual Studio 或 .NET Core 提供的工具来判断。不过请注意,社区已经请求此功能 here,因此您可以在那里发表意见。

目前,最好的选择是按照步骤to find out the id of the process given the application's port:

  1. 运行 netstat -abon | findStr "127.0.0.1:{PORTNUMBER}"
  2. 在上面找到进程的 ID return,为了加快查找速度,名称将是 dotnet.exe

如果你喜欢冒险,你可能想使用像这样的 PowerShell,它将 return 直接端口号:

 $string = netstat -abon | findStr "127.0.0.1:{PORTNUMBER}"; $results = $string.split(' '); $results[$results.length - 1]

你可以print the pid to the console and use that to select from Ctrl-Alt-P

Console.WriteLine($"Running at pid {System.Diagnostics.Process.GetCurrentProcess().Id}");

较早的问题,但这是我们解决它的方法。

从头开始,使用 $ dotnet run 将创建一个 Kestrel 实例,因此很容易从 Visual Studio.

附加到

但是,运行$ dotnet build && dotnet run 将创建一个构建服务器实例和一个 Kestrel 实例。现在很难知道要附加到哪个 dotnet 进程。此外,运行多次执行此命令可以创建额外的进程。

我们的解决方案是使用 $ dotnet build && dotnet build-server shutdown && dotnet run。这会在构建之后停止构建服务器,所以现在只有一个 dotnet 进程可以附加到。

这里有时可能会有其他解决方案:https://github.com/dotnet/cli/issues/9481