示例#1
0
        /// <summary>
        /// Intercept the invocation
        /// </summary>
        /// <param name="invocation"></param>
        public void Intercept(_IInvocation invocation)
        {
            if (!invocation.Method.IsVirtual || invocation.Method.IsFinal)
            {
                invocation.Proceed();
                return;
            }

            var methodExecutingContext = new MethodExecutingContext
            {
                InvocationContext = _contextProvider.GetContext(),
                MethodInfo        = invocation.Method,
                Invocation        = invocation
            };

            var attributes = GetInvocationMethodFilterAttributes(invocation, methodExecutingContext.InvocationContext);

            if (attributes.Any(a => a is NoInterceptAttribute))
            {
                invocation.Proceed();
                return;
            }

            var filterAttributes = attributes.OfType <MethodFilterAttribute>().OrderBy(x => x.Order).ToList();
            var isAsync          = typeof(Task).IsAssignableFrom(invocation.Method.ReturnType);

            if (isAsync)
            {
                if (invocation.Method.ReturnType.IsGenericType && invocation.Method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                {
                    var taskResultType = invocation.Method.ReturnType.GetGenericArguments()[0];
                    var mInfo          = _handleAsyncWithTypeMethod.MakeGenericMethod(taskResultType);
                    filterAttributes.Add(new InvocationAttribute(invocation, taskResultType));
                    invocation.ReturnValue = mInfo.Invoke(this, new object[] { filterAttributes, methodExecutingContext, taskResultType });
                }
                else
                {
                    filterAttributes.Add(new InvocationAttribute(invocation));
                    invocation.ReturnValue = HandleAsync(filterAttributes, methodExecutingContext);
                }
            }
            else
            {
                filterAttributes.Add(new InvocationAttribute(invocation));
                HandleSync(filterAttributes, methodExecutingContext);
            }
        }
示例#2
0
 public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
 {
     _invocation.Proceed();
     methodExecutingContext.Result = _invocation.ReturnValue;
 }