Cant hit breakpoint in web api controller
Solution 1
Try removing the async part of the action. This isn't a permanent solution, but it might help you debug. Another thing I'd suggest is to put a try catch around the code in your action. It's possible your deserialization is failing and throwing an exception that for whatever reason, the debugger isn't catching.
// POST: api/Events
[HttpPost]
public ActionResult PostEvent([FromBody] object savedEvent)
{
Event addedEvent = JsonConvert.DeserializeObject<Event>(savedEvent.ToString());
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Solution 2
Couple of things you may try
1) Make sure you are running in Debug mode (not in release)
2) Make sure you are running the latest code with all symbols loaded (hovering over the break point can give you extra information of why its disabled)
Solution 3
You are probably running your application in the "Release" mode instead of "Debug".
You can't set breakpoints in "Release mode" (most of the times).
What is in a symbol (.pdb) file? The exact contents of symbol files will vary from language to language and based on your compiler settings, but at a very high level they are the record of how the compiler turned your source code into machine code that the processor executes.
Solution 4
What about trying:
System.Diagnostics.Debugger.Launch();
Launches and attaches a debugger to the process. To at least make sure you're getting in there.
Related videos on Youtube
Mind Tha Gap
Updated on October 06, 2022Comments
-
Mind Tha Gap over 1 year
When i try to debug this code :
// POST: api/Events [HttpPost] public async Task<IActionResult> PostEvent([FromBody] object savedEvent) { Event addedEvent = JsonConvert.DeserializeObject<Event>(savedEvent.ToString()); if (!ModelState.IsValid) { return BadRequest(ModelState); }
Cant hit this line :
Event addedEvent = JsonConvert.DeserializeObject<Event>(savedEvent.ToString());
Debuger reacts like i hit continue but code past doesnt execute. Im really confused. Thanks for your help.
-
Mind Tha Gap over 7 yearsI forgot to mention it. Im in debug mode and symbols are loaded.
-
Ali Baig over 7 yearsThere is nothing wrong with the code so it should hit. Can you try stopping the restarting the application, also can you hit any other breakpoints in the application?
-
Ali Baig over 7 yearsThis is strange. One last thing that I can suggest is, remove everything from Bin folder, rebuild the application and run it again
-
Mind Tha Gap over 7 yearsRemoving async helped . It throwed an exception in Newthonsoft library. Now i see debuging async methods can become little tricky. Thanks!
-
Lars Holdgaard over 6 yearsThis saved a lot of time for me.. Thnx!
-
Ali Baig over 6 yearsThanks @LarsHoldgaard, Although irrelevant but I think we've done some work together on your Jarboo project couple of years back, good to see you again!!