I managed to debug an asp .net core 3.1 application within an Ubuntu WSL-2 distro (on Windows 10), using a docker-compose file thanks to this Vs code tutorial
But considering we start the container using a docker-compose up
command, then we attach the debugger, all breakpoints set in Program.cs
or Startup.cs
are not hit. Indeed the app has already pocessed these files when I manually attach the debugger, it's too late.
All other breakpoints (in controllers or background process ...) are correctly hit.
When debugging the same project with Visual Studio 2019 Community, all my breakpoints are hit, even those in Program.cs
or Startup.cs
which is the expected behavior here.
What am I doing wrong ?
The breakpoint l.44 is never hit
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
{
"version": "0.2.0",
"configurations": [
{
"name": "Docker .NET Core Attach (Preview)",
"containerName": "myapp",
"type": "docker",
"request": "attach",
"platform": "netCore",
"sourceFileMap": {
"/src": "${workspaceFolder}"
}
}
]
}
Indeed if I launch the VS code debugger before the container is up, I get the following error message
Error: Process 'docker exec -i "myapp...' exited with code 1
Error: Error: No such container: myapp
I opened an Issue on Github and here the provided responses:
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) ...
... 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
So to conclude, until a solution is provided by Microsoft on VSCode or via an adhoc extension, one solution is to add some waiting code :
public static IHostBuilder CreateHostBuilder(string[] args)
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
{
while (!Debugger.IsAttached) { Thread.Sleep(100); }
}
Now breakpoints in Startup.cs
are correclty hit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With