Beispiel #1
0
        /// <summary>
        /// This will be called via Reflection
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="filterAttributes"></param>
        /// <param name="exceptionFilterAttributes"></param>
        /// <param name="methodExecutingContext"></param>
        /// <returns></returns>
        private async Task <TResult> HandleAsyncWithType <TResult>(IReadOnlyList <MethodFilterAttribute> filterAttributes, IReadOnlyList <ExceptionFilterAttribute> exceptionFilterAttributes, MethodExecutingContext methodExecutingContext)
        {
            foreach (var f in filterAttributes)
            {
                try
                {
                    if (methodExecutingContext.Result == null)
                    {
                        await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    var exContext = new MethodExceptionContext(ex, methodExecutingContext);
                    await HandleExceptionAsync(exceptionFilterAttributes, exContext);

                    if (!exContext.Handled)
                    {
                        throw;
                    }
                }
            }

            var reversedFilterAttributes = filterAttributes.Reverse();
            var methodExecutedContext    = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in reversedFilterAttributes)
            {
                try
                {
                    await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    var exContext = new MethodExceptionContext(ex, methodExecutedContext);
                    await HandleExceptionAsync(exceptionFilterAttributes, exContext);

                    if (!exContext.Handled)
                    {
                        throw;
                    }
                }
            }

            if (methodExecutedContext.Result != null && methodExecutedContext.Result is TResult result)
            {
                return(result);
            }
            return(default(TResult));
        }
Beispiel #2
0
        private void HandleSync(IReadOnlyList <MethodFilterAttribute> filterAttributes, IReadOnlyList <ExceptionFilterAttribute> exceptionFilterAttributes, MethodExecutingContext methodExecutingContext)
        {
            foreach (var f in filterAttributes)
            {
                try
                {
                    if (methodExecutingContext.Result == null)
                    {
                        f.OnMethodExecuting(methodExecutingContext);
                    }
                }
                catch (Exception ex)
                {
                    var exContext = new MethodExceptionContext(ex, methodExecutingContext);
                    HandleException(exceptionFilterAttributes, exContext);
                    if (!exContext.Handled)
                    {
                        throw;
                    }
                }
            }

            var reversedFilterAttributes = filterAttributes.Reverse();
            var methodExecutedContext    = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in reversedFilterAttributes)
            {
                try
                {
                    f.OnMethodExecuted(methodExecutedContext);
                }
                catch (Exception ex)
                {
                    var exContext = new MethodExceptionContext(ex, methodExecutedContext);
                    HandleException(exceptionFilterAttributes, exContext);
                    if (!exContext.Handled)
                    {
                        throw;
                    }
                }
            }
        }
 public virtual Task OnExceptionAsync(MethodExceptionContext exceptionContext)
 {
     OnException(exceptionContext);
     return(Task.CompletedTask);
 }
 public virtual void OnException(MethodExceptionContext exceptionContext)
 {
 }
Beispiel #5
0
 private async Task HandleExceptionAsync(IReadOnlyList <ExceptionFilterAttribute> exceptionFilterAttributes, MethodExceptionContext exceptionContext)
 {
     foreach (var f in exceptionFilterAttributes)
     {
         try
         {
             if (!exceptionContext.Handled)
             {
                 await f.OnExceptionAsync(exceptionContext).ConfigureAwait(false);
             }
         }
         catch (Exception ex)
         {
             throw new AggregateException(ex.Message, ex, exceptionContext.Exception);
         }
     }
 }
Beispiel #6
0
 private void HandleException(IReadOnlyList <ExceptionFilterAttribute> exceptionFilterAttributes, MethodExceptionContext exceptionContext)
 {
     foreach (var f in exceptionFilterAttributes)
     {
         try
         {
             if (!exceptionContext.Handled)
             {
                 f.OnException(exceptionContext);
             }
         }
         catch (Exception ex)
         {
             throw new AggregateException(ex.Message, ex, exceptionContext.Exception);
         }
     }
 }