public async Task OnInvocation(AspectContext aspectContext, AspectDelegate next)
        {
            await next.Invoke(aspectContext);


            //执行异常了不执行after 去执行Throw
            if (aspectContext.Exception != null)
            {
                return;
            }


            if (_afterAttribute != null)
            {
                await this._afterAttribute.AfterReturn(aspectContext, aspectContext.ReturnValue);
            }
            else
            {
                var rt = AutoConfigurationHelper.InvokeInstanceMethod(
                    _pointCutMethod.Instance,
                    _pointCutMethod.MethodInfo,
                    _pointCutMethod.MethodParameters,
                    aspectContext.ComponentContext,
                    aspectContext,
                    returnValue: aspectContext.ReturnValue,
                    returnParam: _pointCutMethod.PointcutBasicAttribute.Returing,
                    injectAnotation: _pointCutMethod.PointcutInjectAnotation);

                if (typeof(Task).IsAssignableFrom(_pointCutMethod.MethodReturnType))
                {
                    await((Task)rt).ConfigureAwait(false);
                }
            }
        }
예제 #2
0
        public async Task OnInvocation(AspectContext aspectContext, AspectDelegate next)
        {
            try
            {
                if (!_isAfterAround)
                {
                    await next.Invoke(aspectContext);
                }
            }
            finally
            {
                //不管成功还是失败都会执行的
                if (_afterAttribute != null)
                {
                    await this._afterAttribute.After(aspectContext, aspectContext.Exception ?? aspectContext.ReturnValue);
                }
                else
                {
                    var rt = AutoConfigurationHelper.InvokeInstanceMethod(
                        _pointCutMethod.Instance,
                        _pointCutMethod.MethodInfo,
                        _pointCutMethod.MethodParameters,
                        aspectContext.ComponentContext,
                        aspectContext,
                        returnValue: aspectContext.Exception ?? aspectContext.ReturnValue,
                        returnParam: _pointCutMethod.PointcutBasicAttribute.Returing,
                        injectAnotation: _pointCutMethod.PointcutInjectAnotation);

                    if (typeof(Task).IsAssignableFrom(_pointCutMethod.MethodReturnType))
                    {
                        await((Task)rt).ConfigureAwait(false);
                    }
                }
            }
        }
예제 #3
0
        public async Task OnInvocation(AspectContext aspectContext, AspectDelegate next)
        {
            Exception exception = null;

            try
            {
                if (_aroundAttribute != null)
                {
                    await _aroundAttribute.OnInvocation(aspectContext, next);

                    return;
                }

                var rt = AutoConfigurationHelper.InvokeInstanceMethod(
                    _pointCutMethod.Instance,
                    _pointCutMethod.MethodInfo,
                    _pointCutMethod.MethodParameters,
                    aspectContext.ComponentContext,
                    aspectContext,
                    next,
                    injectAnotation: _pointCutMethod.PointcutInjectAnotation);

                if (typeof(Task).IsAssignableFrom(_pointCutMethod.MethodReturnType))
                {
                    await((Task)rt).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (exception == null && _aspectAfter != null)
                {
                    await _aspectAfter.OnInvocation(aspectContext, next);
                }
            }

            try
            {
                if (exception != null && _aspectAfter != null)
                {
                    await _aspectAfter.OnInvocation(aspectContext, next);
                }

                if (exception != null && _aspectThrows != null)
                {
                    await _aspectThrows.OnInvocation(aspectContext, next);
                }
            }
            finally
            {
                if (exception != null)
                {
                    throw exception;
                }
            }
        }
        public async Task OnInvocation(AspectContext aspectContext, AspectDelegate next)
        {
            try
            {
                if (!_isFromAround)
                {
                    await next.Invoke(aspectContext);
                }
            }
            finally
            {
                //只有目标方法出现异常才会走 增强的方法出异常不要走
                if (aspectContext.Exception != null)
                {
                    Exception ex = aspectContext.Exception;
                    if (aspectContext.Exception is TargetInvocationException targetInvocationException)
                    {
                        ex = targetInvocationException.InnerException;
                    }

                    if (ex == null)
                    {
                        ex = aspectContext.Exception;
                    }

                    var currentExType = ex.GetType();

                    if (_pointcutThrowin != null)
                    {
                        if (_pointcutThrowin.PointcutBasicAttribute.ExceptionType == null || _pointcutThrowin.PointcutBasicAttribute.ExceptionType == currentExType)
                        {
                            var rt = AutoConfigurationHelper.InvokeInstanceMethod(
                                _pointcutThrowin.Instance,
                                _pointcutThrowin.MethodInfo,
                                _pointcutThrowin.MethodParameters,
                                aspectContext.ComponentContext,
                                aspectContext,
                                returnValue: ex,
                                returnParam: _pointcutThrowin.PointcutBasicAttribute.Throwing,
                                injectAnotation: _pointcutThrowin.PointcutInjectAnotation);
                            if (typeof(Task).IsAssignableFrom(_pointcutThrowin.MethodReturnType))
                            {
                                await((Task)rt).ConfigureAwait(false);
                            }
                        }
                    }
                    else
                    {
                        if (_aspectThrowing.ExceptionType == null || _aspectThrowing.ExceptionType == currentExType)
                        {
                            await _aspectThrowing.AfterThrows(aspectContext, aspectContext.Exception);
                        }
                    }
                }
            }
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="proceedInfo"></param>
        /// <param name="proceed"></param>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            if (!_configuration.PointcutTargetInfoList.TryGetValue(invocation.MethodInvocationTarget, out var pointCut))
            {
                //该方法不需要拦截
                return(await proceed(invocation, proceedInfo));
            }

            //pointcut定义所在对象
            var instance = _component.Resolve(pointCut.PointClass);

            AspectContext aspectContext = new AspectContext
            {
                ComponentContext      = _component,
                InvocationContext     = invocation,
                InvocationProceedInfo = proceedInfo
            };

            if (pointCut.AroundMethod != null)
            {
                return(AutoConfigurationHelper.InvokeInstanceMethodReturn <TResult>(instance, pointCut.AroundMethod, _component, aspectContext));
            }

            try
            {
                if (pointCut.BeforeMethod != null)
                {
                    AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.BeforeMethod, _component, aspectContext);
                }

                var rt = await proceed(invocation, proceedInfo);


                if (pointCut.AfterMethod != null)
                {
                    AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                }

                return(rt);
            }
            catch (Exception e)
            {
                aspectContext.Exception = e;
                if (pointCut.AfterMethod != null)
                {
                    AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                }
                throw;
            }
        }
        public async Task OnInvocation(AspectContext aspectContext, AspectDelegate next)
        {
            if (_beforeAttribute != null)
            {
                await this._beforeAttribute.Before(aspectContext);
            }
            else
            {
                var rt = AutoConfigurationHelper.InvokeInstanceMethod(
                    _pointCutMethod.Instance,
                    _pointCutMethod.MethodInfo,
                    _pointCutMethod.MethodParameters,
                    aspectContext.ComponentContext, aspectContext, injectAnotation: _pointCutMethod.PointcutInjectAnotation);
                if (typeof(Task).IsAssignableFrom(_pointCutMethod.MethodReturnType))
                {
                    await((Task)rt).ConfigureAwait(false);
                }
            }

            await next.Invoke(aspectContext);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="proceedInfo"></param>
        /// <param name="proceed"></param>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            if (!_configuration.PointcutTargetInfoList.TryGetValue(invocation.MethodInvocationTarget, out var pointCut))
            {
                if (!invocation.MethodInvocationTarget.DeclaringType.GetTypeInfo().IsGenericType || !_configuration.DynamicPointcutTargetInfoList.TryGetValue(invocation.MethodInvocationTarget.GetMethodInfoUniqueName(), out var pointCutDynamic))
                {
                    //该方法不需要拦截
                    return(await proceed(invocation, proceedInfo));
                }

                pointCut = pointCutDynamic;
            }

            //pointcut定义所在对象
            var instance = _component.Resolve(pointCut.PointClass);

            PointcutContext aspectContext = new PointcutContext
            {
                ComponentContext = _component,
                InvocationMethod = invocation.MethodInvocationTarget,
            };

            if (pointCut.AroundMethod != null)
            {
                aspectContext.Proceed = async() =>
                {
                    invocation.ReturnValue = await proceed(invocation, proceedInfo);
                };

                var rt = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AroundMethod, _component, aspectContext);
                if (typeof(Task).IsAssignableFrom(pointCut.AroundMethod.ReturnType))
                {
                    await((Task)rt).ConfigureAwait(false);
                }

                return((TResult)invocation.ReturnValue);
            }

            try
            {
                if (pointCut.BeforeMethod != null)
                {
                    var rtBefore = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.BeforeMethod, _component, aspectContext);
                    if (typeof(Task).IsAssignableFrom(pointCut.BeforeMethod.ReturnType))
                    {
                        await((Task)rtBefore).ConfigureAwait(false);
                    }
                }

                var rt = await proceed(invocation, proceedInfo);


                if (pointCut.AfterMethod != null)
                {
                    var rtAfter = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                    if (typeof(Task).IsAssignableFrom(pointCut.AfterMethod.ReturnType))
                    {
                        await((Task)rtAfter).ConfigureAwait(false);
                    }
                }

                return(rt);
            }
            catch (Exception e)
            {
                aspectContext.Exception = e;
                if (pointCut.AfterMethod != null)
                {
                    var rtAfter = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                    if (typeof(Task).IsAssignableFrom(pointCut.AfterMethod.ReturnType))
                    {
                        await((Task)rtAfter).ConfigureAwait(false);
                    }
                }
                throw;
            }
        }
예제 #8
0
        /// <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)
        {
            if (!_configuration.PointcutTargetInfoList.TryGetValue(invocation.MethodInvocationTarget, out var pointCut))
            {
                //该方法不需要拦截
                await proceed(invocation, proceedInfo);

                return;
            }

            //pointcut定义所在对象
            var instance = _component.Resolve(pointCut.PointClass);

            PointcutContext aspectContext = new PointcutContext
            {
                ComponentContext = _component,
                InvocationMethod = invocation.MethodInvocationTarget,
            };

            if (pointCut.AroundMethod != null)
            {
                aspectContext.Proceed = async() =>
                {
                    await proceed(invocation, proceedInfo);
                };

                var rt = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AroundMethod, _component, aspectContext);
                if (typeof(Task).IsAssignableFrom(pointCut.AroundMethod.ReturnType))
                {
                    await((Task)rt).ConfigureAwait(false);
                }
                return;
            }

            try
            {
                if (pointCut.BeforeMethod != null)
                {
                    var rtBefore = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.BeforeMethod, _component, aspectContext);
                    if (typeof(Task).IsAssignableFrom(pointCut.BeforeMethod.ReturnType))
                    {
                        await((Task)rtBefore).ConfigureAwait(false);
                    }
                }
                await proceed(invocation, proceedInfo);

                if (pointCut.AfterMethod != null)
                {
                    var rtAfter = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                    if (typeof(Task).IsAssignableFrom(pointCut.AfterMethod.ReturnType))
                    {
                        await((Task)rtAfter).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                aspectContext.Exception = e;
                if (pointCut.AfterMethod != null)
                {
                    var rtAfter = AutoConfigurationHelper.InvokeInstanceMethod(instance, pointCut.AfterMethod, _component, aspectContext);
                    if (typeof(Task).IsAssignableFrom(pointCut.AfterMethod.ReturnType))
                    {
                        await((Task)rtAfter).ConfigureAwait(false);
                    }
                }
                throw;
            }
        }