0

I have created a standard ASP.NET website targeting .NET Framework and am running it in Docker using Docker-compose.

When debugging the website, none of the breakpoints in the "startup" code of the website are hit.

Breakpoints in controllers are hit as expected.

When running in IISExpress, I hit the breakpoints in startup code immediately.

To me, it seems that VS 2019 doesn't attach to the website in time for it to hit the breakpoints in the startup code.

I've tried the same thing with a ASP.NET Core website, and there the breakpoints in Program.Main()-method are hit immediately.

I have tried setting the Docker-compose "Launch Action" to "Do Nothing" to avoid the website from starting up until I make a request to it, but with no luck.

Dockerfile:

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.7.2-windowsservercore-1803
ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .

Docker-compose.yml:

version: '3.4'

services:
  mydockerizedwebapp:
    image: ${DOCKER_REGISTRY-}mydockerizedwebapp
    container_name: mydockerizedwebapp
    build:
      context: .\MyDockerizedWebApp
      dockerfile: Dockerfile

Docker-compose.override.yml:

version: '3.4'

services:
  mydockerizedwebapp:
    ports:
      - "80"
networks:
  default:
    external:
      name: nat

Does anyone know how to attach the Visual Studio debugger in time for me to hit breakpoints in the startup code?

Edit:

If an exception occurs in the startup of the web site, so that it goes down, then the next request to the webserver will "reboot" it, and the breakpoints are hit. But why don't they hit the first time around?

Eirik Fauske
  • 445
  • 5
  • 16

1 Answers1

0

You can make you application wait for a debugger to attach:

#if Debug_Break
while (!Debugger.IsAttached)
{
    Thread.Sleep(500);
}
Debugger.Break();
#endif

You can add a Debug_Break configuration or you can just remove the #if. Put this code in the startup section.

Note that you DO NOT want this code in your release binaries for obvious reasons.

fstam
  • 669
  • 4
  • 20
  • This does not work. When I add the while-loop, the Visual Studio debugger is not able to connect at all. VS just hangs for a while before I get the error message: "Unable to start debugging on the web server. The operation has timed out". – Eirik Fauske May 21 '19 at 13:10
  • Try it in a console application? Also see [this](https://stackoverflow.com/questions/361077/how-to-wait-until-remote-net-debugger-attached) – fstam May 21 '19 at 13:31
  • I don't understand what you mean by "Try it in a console application?". I am sure it works in a console application, but I need it to work in an ASP.NET website running in Docker. – Eirik Fauske May 21 '19 at 13:42