public async Task Around(PointcutContext context)
        {
            Console.WriteLine(context.InvocationMethod.Name + "-->Start");
            await context.Proceed();

            Console.WriteLine(context.InvocationMethod.Name + "-->End");
        }
Example #2
0
        public async Task RunWithTransaction(PointcutContext aspectContext)
        {
            logger.Debug($"start transactionScope on `{aspectContext.InvocationMethod.DeclaringType.FullName + "." + aspectContext.InvocationMethod.Name}`");
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                await aspectContext.Proceed();

                if (Transaction.Current.TransactionInformation.Status == TransactionStatus.Active)
                {
                    scope.Complete();
                    logger.Debug($"submit transactionScope on `{aspectContext.InvocationMethod.DeclaringType.FullName + "." + aspectContext.InvocationMethod.Name}`");
                }
            }
        }
Example #3
0
        public static object InvokeInstanceMethod(object instance, MethodInfo methodInfo, IComponentContext context, PointcutContext invocation = null)
        {
            try
            {
                var parameters = methodInfo.GetParameters();
                if (parameters.Length == 0)
                {
                    return(methodInfo.Invoke(instance, null));
                }

                //自动类型注入

                List <object> parameterObj = new List <object>();
                foreach (var parameter in parameters)
                {
                    if (invocation != null && parameter.ParameterType == typeof(PointcutContext))
                    {
                        parameterObj.Add(invocation);
                        continue;
                    }

                    var autowired = parameter.GetCustomAttribute <Autowired>();
                    if (autowired != null)
                    {
                        parameterObj.Add(autowired.ResolveParameter(parameter, context));
                        continue;
                    }

                    var value = parameter.GetCustomAttribute <Value>();
                    if (value != null)
                    {
                        parameterObj.Add(value.ResolveParameter(parameter, context));
                        continue;
                    }

                    if (parameter.HasDefaultValue)
                    {
                        parameterObj.Add(parameter.RawDefaultValue);
                        continue;
                    }

                    if (parameter.IsOptional)
                    {
                        parameterObj.Add(Type.Missing);
                        continue;
                    }

                    if (parameter.IsOut)
                    {
                        parameterObj.Add(Type.Missing);
                        continue;
                    }

                    if (parameter.ParameterType.IsValueType || parameter.ParameterType.IsEnum)
                    {
                        parameterObj.Add(parameter.RawDefaultValue);
                        continue;
                    }



                    parameterObj.Add(context.Resolve(parameter.ParameterType));
                }

                return(methodInfo.Invoke(instance, parameterObj.ToArray()));
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"The class `{methodInfo.DeclaringType.FullName}` method `{methodInfo.Name}` invoke fail!", e);
            }
        }
        /// <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;
            }
        }
        /// <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;
            }
        }