I've a problem with ASP.NET MVC controller and async operation on EF context. If a problem occurs (an exception) on entityContext, I can't see/capture/debug the exception, the execution returns immediatly on asp.net pipeline and (in my case) I see the 500.html page. This is the code where I do not have any control:
RENEWAL renewal;
using (var db = new MyEntities()) //Breakpoint #1
{
try
{
renewal = await db.RENEWAL.FindAsync(id, ctr);
}
catch (AggregateException ex)
{ //breakpoint #2
throw;
}
return View(renewal); //Breakpoint #3
}
I can debug with Breakpoint #1, and on "FindAsync" operation there is an error but, both Breakpoint #2 and #3 are never reached.
So, I've modified the code like this:
RENEWAL renewal;
using (var db = new MyEntities())
{
try
{
renewal = await db.RENEWAL.FindAsync(id, ctr)
.ContinueWith(t =>
{
Debug.Print("Continue With");
return t.Result; //breakpoint #2
});
}
catch (AggregateException ex)
{ //breakpoint #3
throw;
}
return View(renewal); //Breakpoint #4
}
Now Breakpoints #2 and #3 are working.
I didn't quite understand how async / await works in an MVC controller. Can you tell me the difference between the first and the second block, and if there is a correct way to manage these operations towards EF from controller? Thanks