user10541338
user10541338

Reputation: 13

what am I doing wrong with this dotnet core debug setup?

I'm fairly new to dotnet core and I just started on a new project which has an existing app written in dotnet core. There is an existing console app written in dotnet core. I can run the app within VS in debug mode and the breakpoint on the first line of code in the Main() method gets hit. I need to follow some instructions in the documentation which state that the app should be executable from a powershell console as "dotnet run -- -a". I tried "Debug > Attach to Process > dotnet.exe" within the console app but when I execute the "dotnet run -- -a" command via the powershell console, the breakpoint on the first line of code in the Main() method doesn't get hit. Any idea what I'm doing wrong here?

Upvotes: 0

Views: 439

Answers (1)

poke
poke

Reputation: 388383

When you use the “Attach to Process” functionality in Visual Studio, you attach the debugger to an already running process. Since you start the process first using dotnet run, it is very likely that by the time the debugger is attached, the beginning of your application has already ran.

That makes “Attach to Process” not so suited for debugging purposes where you need the debugger to be around during the application start. Instead, it is more useful for applications that can already run for a while and where doing something triggers the behavior you want to debug. For example, with web applications, you can just start the web application and then attach to it, and only by making a request to the web application you are hitting the code you want to debug.

If you want to debug an application during its startup, and you cannot run the application directly from within Visual Studio, then you can use a custom debug target.

To do that, open the project properties and go to the “Debug” tab. There, choose “Executable” as the “Launch” type and then specify the required settings to launch your application:

Running dotnet.exe as the executable debug target

Then, when you hit F5 to start debugging the application, that executable is launched instead of the project. That way, you should be able to debug your application properly even within its Main method.

Upvotes: 1

Related Questions