private static void InterceptSynchronousVoid(AsyncInterceptorBase me, IInvocation invocation) { Task task = me.InterceptAsync(invocation, ProceedSynchronous); // If the intercept task has yet to complete, wait for it. if (!task.IsCompleted) { Task.Run(() => task).Wait(); } if (task.IsFaulted) { if (task.Exception != null) { if (task.Exception.InnerException != null) { throw task.Exception.InnerException; } else { throw task.Exception; } } } }
private static void InterceptSynchronousResult <TResult>(AsyncInterceptorBase me, IInvocation invocation) { Task <TResult> task = me.InterceptAsync(invocation, invocation.CaptureProceedInfo(), ProceedSynchronous <TResult>); // If the intercept task has yet to complete, wait for it. if (!task.IsCompleted) { // Need to use Task.Run() to prevent deadlock in .NET Framework ASP.NET requests. // GetAwaiter().GetResult() prevents a thrown exception being wrapped in a AggregateException. // See https://stackoverflow.com/a/17284612 Task.Run(() => task).GetAwaiter().GetResult(); } task.RethrowIfFaulted(); }
private static void InterceptSynchronousResult <TResult>(AsyncInterceptorBase me, IInvocation invocation) { try { Task <TResult> task = Task.Run(() => me.InterceptAsync(invocation, invocation.CaptureProceedInfo(), ProceedSynchronous <TResult>)); invocation.ReturnValue = task.GetAwaiter().GetResult(); //TODO: check // Task<TResult> task = me.InterceptAsync(invocation, invocation.CaptureProceedInfo(), ProceedSynchronous<TResult>); // if (!task.IsCompleted) // { // // Need to use Task.Run() to prevent deadlock in .NET Framework ASP.NET requests. // // GetAwaiter().GetResult() prevents a thrown exception being wrapped in a AggregateException. // // See https://stackoverflow.com/a/17284612 // Task.Run(() => task).GetAwaiter().GetResult(); // } } catch (Exception ex) { throw ex; } }