I have an async action, actually from ASP.net Identity, which is throwing an exception in an async database query. However this exception does not seem to be making it to the general application_error I have in place in Global.asax.cs. Other exceptions are being caught correctly.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName.Trim(), model.Password.Trim());
...
}
}
In my case the database had been changed without updating the models - but I would really like a mechanism which I can apply globally to all the various async actions I have.
I did see another post which suggested hooking into the unobserved exception on the task scheduler like so:
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
Console.WriteLine("Saving the day! This exception would have been unobserved: {0}",
e.Exception);
e.SetObserved();
};
However break points inside the lambda are never hit. I can intercept the exception with a try/catch block around the async but that is highly explicit and a pain to implement everywhere.
How can I catch and, at the least, log these exceptions?