Exemplo n.º 1
0
        /// <summary>
        /// 有返回值拦截器
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="invocation"></param>
        /// <param name="proceed"></param>
        /// <returns></returns>
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation, Func <IInvocation, Task <TResult> > proceed)
        {
            var attribute = await BeforeInterceptAttribute(invocation);

            try
            {
                TResult r;

                if (attribute == null)
                {
                    r = await proceed(invocation);

                    return(r);
                }

                if (attribute.Item3 != null)
                {
                    await AfterInterceptAttribute(attribute.Item2, invocation, attribute.Item3);

                    throw attribute.Item3;
                }


                if (attribute.Item1 == null || !attribute.Item1.Any())
                {
                    r = await proceed(invocation);
                }
                else
                {
                    AspectMiddlewareBuilder builder = new AspectMiddlewareBuilder();
                    foreach (var pointAspect in attribute.Item1)
                    {
                        builder.Use(next => async ctx => { await pointAspect.OnInvocation(ctx, next); });
                    }


                    builder.Use(next => async ctx =>
                    {
                        ctx.Result = await proceed(invocation);
                    });

                    var aspectfunc    = builder.Build();
                    var aspectContext = new AspectContext(_component, invocation);
                    await aspectfunc(aspectContext);

                    r = (TResult)aspectContext.Result;
                }

                await AfterInterceptAttribute(attribute.Item2, invocation, null);

                return(r);
            }
            catch (Exception e)
            {
                await AfterInterceptAttribute(attribute.Item2, invocation, e);

                throw;
            }
        }
        /// <summary>
        /// 无返回值拦截器
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="proceedInfo"></param>
        /// <param name="proceed"></param>
        /// <returns></returns>
        protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task> proceed)
        {
            var attribute = await BeforeInterceptAttribute(invocation);

            try
            {
                if (attribute == null)
                {
                    await proceed(invocation, proceedInfo);

                    return;
                }

                if (attribute.Item3 != null)
                {
                    await AfterInterceptAttribute(attribute.Item2, invocation, attribute.Item3);

                    throw attribute.Item3;
                }

                if (attribute.Item1 == null || !attribute.Item1.Any())
                {
                    await proceed(invocation, proceedInfo);
                }
                else
                {
                    AspectMiddlewareBuilder builder = new AspectMiddlewareBuilder();
                    foreach (var pointAspect in attribute.Item1)
                    {
                        builder.Use(next => async ctx => { await pointAspect.OnInvocation(ctx, next); });
                    }

                    builder.Use(next => async ctx => { await proceed(invocation, proceedInfo); });

                    var aspectfunc = builder.Build();
                    await aspectfunc(new AspectContext(_component, invocation));
                }
                await AfterInterceptAttribute(attribute.Item2, invocation, null);
            }
            catch (Exception e)
            {
                if (attribute != null)
                {
                    await AfterInterceptAttribute(attribute.Item2, invocation, e);
                }
                throw;
            }
        }
        /// <summary>
        /// 有返回值拦截器
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="invocation"></param>
        /// <param name="proceedInfo"></param>
        /// <param name="proceed"></param>
        /// <returns></returns>
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            var attribute = await BeforeInterceptAttribute(invocation);

            try
            {
                TResult r;

                if (attribute == null)
                {
                    r = await proceed(invocation, proceedInfo);

                    return(r);
                }

                if (attribute.Item3 != null)
                {
                    await AfterInterceptAttribute(attribute.Item2, invocation, attribute.Item3);

                    throw attribute.Item3;
                }


                if (attribute.Item1 == null || !attribute.Item1.Any())
                {
                    r = await proceed(invocation, proceedInfo);
                }
                else
                {
                    AspectMiddlewareBuilder builder = new AspectMiddlewareBuilder();
                    foreach (var pointAspect in attribute.Item1)
                    {
                        builder.Use(next => async ctx =>
                        {
                            await pointAspect.OnInvocation(ctx, next);
                            //如果有拦截器设置 ReturnValue 那么就直接拿这个作为整个拦截器的方法返回值
                            if (ctx.InvocationContext.ReturnValue != null)
                            {
                                ctx.Result = ctx.InvocationContext.ReturnValue;
                            }
                        });
                    }


                    builder.Use(next => async ctx =>
                    {
                        ctx.Result             = await proceed(invocation, proceedInfo);
                        invocation.ReturnValue = ctx.Result; //原方法的执行返回值
                    });

                    var aspectfunc    = builder.Build();
                    var aspectContext = new AspectContext(_component, invocation);
                    await aspectfunc(aspectContext);

                    r = (TResult)aspectContext.Result;
                }

                await AfterInterceptAttribute(attribute.Item2, invocation, null);

                return(r);
            }
            catch (Exception e)
            {
                if (attribute != null)
                {
                    await AfterInterceptAttribute(attribute.Item2, invocation, e);
                }
                throw;
            }
        }