/// <summary>
        /// 执行前置拦截器
        /// </summary>
        /// <param name="invocation"></param>
        private async Task <Tuple <List <AspectPointAttribute>, List <AspectInvokeAttribute>, Exception> > BeforeInterceptAttribute(IInvocation invocation)
        {
            //先从缓存里面拿到这个方法时候打了继承AspectInvokeAttribute的标签
            if (!_cache.CacheList.TryGetValue(invocation.MethodInvocationTarget, out var Attributes) || Attributes == null || !Attributes.Any())
            {
                //动态泛型类
                if (!invocation.MethodInvocationTarget.DeclaringType.GetTypeInfo().IsGenericType || (!_cache.DynamicCacheList.TryGetValue(invocation.MethodInvocationTarget.GetMethodInfoUniqueName(), out var AttributesDynamic) || AttributesDynamic == null || !AttributesDynamic.Any()))
                {
                    return(null);
                }

                Attributes = AttributesDynamic;
            }

            var       aspectContext = new AspectContext(_component, invocation);
            Exception ex            = null;

            try
            {
                foreach (var attribute in Attributes)
                {
                    //如果一个方法上面既有AspectAroundAttribute 又有 AspectBeforeAttribute 的话 按照下面的优先级 抛弃 AspectBeforeAttribute
                    switch (attribute)
                    {
                    case AspectAroundAttribute aspectAroundAttribute:
                        await aspectAroundAttribute.Before(aspectContext);

                        break;

                    case AspectBeforeAttribute aspectBeforeAttribute:
                        await aspectBeforeAttribute.Before(aspectContext);

                        break;
                    }
                }
            }
            catch (Exception e)
            {
                ex = e;
            }
            return(new Tuple <List <AspectPointAttribute>, List <AspectInvokeAttribute>, Exception>(Attributes.OfType <AspectPointAttribute>().ToList(), Attributes, ex));
        }
Пример #2
0
        private async Task AfterInterceptAttribute(List <AspectInvokeAttribute> Attributes, IInvocation invocation, Exception exp)
        {
            var aspectContext = new AspectContext(_component, invocation, exp);

            foreach (var attribute in Attributes)
            {
                switch (attribute)
                {
                case AspectAroundAttribute aspectAroundAttribute:
                    await aspectAroundAttribute.After(aspectContext);

                    break;

                case AspectAfterAttribute aspectAfterAttribute:
                    await aspectAfterAttribute.After(aspectContext);

                    break;
                }
            }
        }
        /// <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;
            }
        }
Пример #4
0
 /// <summary>
 /// 后置执行
 /// </summary>
 /// <param name="aspectContext"></param>
 public abstract Task After(AspectContext aspectContext);
Пример #5
0
 /// <summary>
 /// 前置执行
 /// </summary>
 /// <param name="aspectContext"></param>
 public abstract Task Before(AspectContext aspectContext);
Пример #6
0
 /// <summary>
 /// 拦截器
 /// </summary>
 /// <param name="aspectContext">拦截上下文</param>
 /// <param name="_next">下一个拦截器 最后一个是执行被拦截的方法</param>
 /// <returns></returns>
 public abstract Task OnInvocation(AspectContext aspectContext, AspectDelegate _next);