gpt4 book ai didi

docker - 如何在 VsCode 的 docker 中调试我的 Startup.cs(调试器附加得太晚了)

转载 作者:行者123 更新时间:2023-12-05 04:57:13 30 4
gpt4 key购买 nike

由于这个 Vs code tutorial,我使用 docker-compose 文件成功调试了 Ubuntu WSL-2 发行版(在 Windows 10 上)中的 asp .net core 3.1 应用程序

但考虑到我们使用 docker-compose up 命令启动容器,然后我们附加调试器,所有断点都在 Program.csStartup 中设置。 cs 没有被击中。事实上,当我手动附加调试器时,应用程序已经处理了这些文件,为时已晚。所有其他断点(在 Controller 或后台进程中......)都被正确击中。

当使用 Visual Studio 2019 Community 调试同一个项目时,我的所有断点都被命中,甚至是 Program.csStartup.cs 中的断点,这是此处的预期行为.

我做错了什么?

从未命中断点 l.44 The breakpoint l.44 is never hit

  • docker-compose.debug.yml(由 docker-compose -f "docker-compose.debug.yml"up -d --build 启动)
version: '3.7'

services:
myapp:
image: myapp
container_name: myapp
build:
context: .
dockerfile: src/MyApp/src/Dockerfile
ports:
- "60713:80"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80
volumes:
- ~/.vsdbg:/remote_debugger:rw
networks:
default:
external:
name: mynetwork
  • 启动.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Docker .NET Core Attach (Preview)",
"containerName": "myapp",
"type": "docker",
"request": "attach",
"platform": "netCore",
"sourceFileMap": {
"/src": "${workspaceFolder}"
}
}
]
}

确实,如果我在容器启动之前启动 VS 代码调试器,我会收到以下错误消息

Error: Process 'docker exec -i "myapp...' exited with code 1
Error: Error: No such container: myapp

最佳答案

我打开了一个 Issue on Github此处提供的回复:

  1. 来自布兰登滑铁卢 [MSFT]

Right now our only supported story for that is to launch it manually and then attach.We have an issue to add "real" compose debugging (microsoft/vscode-docker#840) ...

  1. 来自 Gregg Miskelly

... the only way to debug startup code is to add something like: while (!Debugger.IsAttached) { Thread.Sleep(100); } to the beginning of your startup code so that it will not proceed until a debugger is attached

总而言之,在 Microsoft 在 VSCode 上或通过临时扩展提供解决方案之前,一种解决方案是添加一些等待代码:

public static IHostBuilder CreateHostBuilder(string[] args)
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
{
while (!Debugger.IsAttached) { Thread.Sleep(100); }
}

现在 Startup.cs 中的断点正确命中 enter image description here

关于docker - 如何在 VsCode 的 docker 中调试我的 Startup.cs(调试器附加得太晚了),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64553243/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com