简体   繁体   中英

Why does this async controller action hang while debugging?

Ever since I started working an MVC project with ASYNC from start - all controllers, database calls, etc. are async and awaited I have been in debug hell. I never get helpful debug/exception information - it's lost in the bowels of the system. I have to go into ELMAH or AI to view the exception since it never bubbles up and ends the request.

In most cases, the page will just spin away until the browser gives up. Am I missing something somewhere? For instance, I have an error on the page (null reference), but since the page is async, I never see the exception.

Unlike most of the questions asked here that tend to cause a deadlock, there is no use of .Result anywhere in this codebase. This is async form the start and all the way through.

To clarify, here is the exact workflow of this MVC controller. I made a few changes to test when it hangs and when it doesn't.

Controller

public async Task<ActionResult> Review(Guid id)
{
    var quote = await Quotes.GetQuoteById(id);
    return View();
}

Service

return await Ef.DbSet.Where(d => d.Id == id).FirstOrDefaultAsync();

View (first line)

@{ throw new ApplicationException("you failed"); }

With the above setup I will never see the exception. It just hangs. However, if I change the controller to this:

return View();
var quote = await Quotes.GetQuoteById(id);

Then I get my exception. If I change the service to non-async I also get to the exception. Therefore, unlike the duplicate question referenced, I don't have an unawaited call anywhere. But the async/await is what is causing the browser to hang.

Is there a way to debug this? As an interesting aside, this only happens during debug, in production, the exception will get raised as expected.

Re-asked/Clarified/Non-Duplicate This was originally asked here but it was closed as duplicate of this question , but it isn't. Added clarification to show why it isn't. But since they closed as a definite duplicate, you can't even see it, so I had to ask as a new question.

It was related to Application Insights by Microsoft/Azure. I have to look into it a bit more (since I just removed all AI references and I need to re-install to investigate), but it might be an unawaited async call I am making into AI or a bug inside AI (it is pre-release). I am only making three calls into AI manually and I didn't receive any warning about async calls not being awaited, so I do not think this is the case and is probably a bug inside AI or by design.

Uninstalling AI let the exception flow as it normally does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM