Example #1
0
        public virtual async Task <object> InvokeAsync(object[] arguments)
        {
            var requestExecutionDelegate = GetRequestExecutionDelegate(arguments);

            RequestExecutedContext requestExecutedContext = null;

            try
            {
                await InitializeRequestAsync(RequestContext.GoContext.Request, arguments);

                requestExecutedContext = await requestExecutionDelegate();

                Rethrow(requestExecutedContext);
                return(requestExecutedContext?.Result);
            }
            catch (Exception e)
            {
                var exceptionInterceptorContext = new ExceptionInterceptorContext(RequestContext, Interceptors)
                {
                    ExceptionDispatchInfo = ExceptionDispatchInfo.Capture(e),
                    Result = requestExecutedContext?.Result
                };

                var exceptionInterceptors = Interceptors
                                            .OfType <IAsyncExceptionInterceptor>()
                                            .Reverse()
                                            .ToArray();
                foreach (var interceptor in exceptionInterceptors)
                {
                    await interceptor.OnExceptionAsync(exceptionInterceptorContext);
                }

                Rethrow(exceptionInterceptorContext);

                return(exceptionInterceptorContext.Result);
            }
            finally
            {
                if (RequestContext.GoContext.RequestServices is IDisposable disposable)
                {
                    disposable.Dispose();
                }
            }
        }
Example #2
0
        private static void Rethrow(ExceptionInterceptorContext context)
        {
            if (context == null)
            {
                return;
            }

            if (context.ExceptionHandled)
            {
                return;
            }

            context.ExceptionDispatchInfo?.Throw();

            if (context.Exception != null)
            {
                throw context.Exception;
            }
        }
 public Task OnExceptionAsync(ExceptionInterceptorContext context)
 {
     return(_exception == null ? Task.CompletedTask : _exception(context));
 }