ExceptionCallHandler
是Enterprise Library中的默认异常调用处理程序,但它不能与异步方法一起使用。
我应该如何重写处理异步方法的处理程序?
这是我的代码,但它不起作用。
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
if (input == null)
throw new ArgumentNullException("input");
if (getNext == null)
throw new ArgumentNullException("getNext");
IMethodReturn methodReturn = getNext()(input, getNext);
MethodInfo methodInfo = input.MethodBase as MethodInfo;
bool isTask = methodInfo != null && typeof(Task).IsAssignableFrom(methodInfo.ReturnType);
Task continueTask = null;
if (isTask)
{
Task task = methodReturn.ReturnValue as Task;
if (task != null)
{
continueTask = task.ContinueWith((t) =>
{
if (t.Exception != null)
{
methodReturn.Exception = t.Exception.Flatten().InnerException;
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
if (continueTask != null)
{
continueTask.Wait();
}
HandleMethodReturnException(methodReturn, input);
return methodReturn;
}