Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug my Startup.cs within docker in VsCode (debugger is attached too late)

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 The breakpoint l.44 is never hit

  • docker-compose.debug.yml (launched by 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
  • launch.json
{
   "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
like image 731
Cladoo Avatar asked Oct 16 '25 02:10

Cladoo


1 Answers

I opened an Issue on Github and here the provided responses:

  1. From Brandon Waterloo [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. From 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

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 enter image description here

like image 141
Cladoo Avatar answered Oct 17 '25 15:10

Cladoo