8

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.

Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44
Mind Tha Gap
  • 125
  • 1
  • 2
  • 6

6 Answers6

6

Couple of things you may try

1) Make sure you are running in Debug mode (not in release)

Like this

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)

Ali Baig
  • 3,819
  • 4
  • 34
  • 47
  • I forgot to mention it. Im in debug mode and symbols are loaded. – Mind Tha Gap Jan 21 '17 at 15:15
  • There 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 Jan 21 '17 at 15:17
  • This is strange. One last thing that I can suggest is, remove everything from Bin folder, rebuild the application and run it again – Ali Baig Jan 21 '17 at 15:42
  • This saved a lot of time for me.. Thnx! – Lars Holdgaard Aug 25 '17 at 20:06
  • Thanks @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!! – Ali Baig Aug 29 '17 at 19:14
4

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);
    }
Brendan Long
  • 276
  • 1
  • 6
  • Removing async helped . It throwed an exception in Newthonsoft library. Now i see debuging async methods can become little tricky. Thanks! – Mind Tha Gap Jan 21 '17 at 16:16
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.

Source: https://blogs.msdn.microsoft.com/devops/2015/01/05/understanding-symbol-files-and-visual-studios-symbol-settings/

tedi
  • 6,350
  • 5
  • 52
  • 67
1

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.

TBD
  • 771
  • 1
  • 11
  • 27
0

I've found that sometimes cleaning and rebuilding helps with flakey breakpoints. If that doesn't help, restarting Visual Studio can also help (especially with 2015, at least in my experience).

Beyond that, I've also had issues with memory leaks causing my app to just enter reach certain parts of my code, though that is usually accompanied with the app crashing.

You didn't mention any errors or crashes occurring, just the code not executing, so I'm not sure if the memory leak would be causing your issue.

0

Wow, I finally solved it. In my case it was incorrectly registered service in DI, just for example:

services.AddScoped<AccountService>();  << 'Fixed my problem'
services.AddScoped<NI.JwtBearerAuth.IAccountService, AccountService>(); << 'Confused me, thought I had already registered it, but as you see, this is other implementation'

After that I was able to hit breakpoint in controller. So, my controller wasn't registered because of this service.

Tropin Alexey
  • 606
  • 1
  • 7
  • 16