I've got this...
public void FooAsync()
{
AsyncManager.OutstandingOperations.Increment();
Task.Factory.StartNew(() =>
{
try
{
doSomething.Start();
}
catch (Exception e)
{
AsyncManager.Parameters["exc"] = e;
}
finally
{
AsyncManager.OutstandingOperations.Decrement();
}
});
}
public ActionResult FooCompleted(Exception exc)
{
if (exc != null)
{
throw exc;
}
return View();
}
Is there a better way of passing an exception back to ASP.net?
Cheers, Ian.
Try putting an attribute like this in FooAsync action:
This way you can create a view to display the detailed error to the user.
Task
will catch the exceptions for you. If you calltask.Wait()
, it will wrap any caught exceptions in anAggregateException
and throw it.Simply adding a
[HandleError]
attribute isn't good enough. Since the exception occurs in a different thread, we have to get the exception back to the ASP.NET thread in order to do anything with it. Only after we have the exception thrown from the right place will the[HandleError]
attribute be able to do its job.