/// <inheritdoc />
        public void Intercept(IInvocation invocation)
        {
            // Map the configured interceptors to this type based on its attributes
            var invocationMetadataCollection = InvocationExtensions.GetInterceptorMetadataForMethod(invocation, this.serviceProvider, this.proxyConfiguration);

            // If there are no configured interceptors, leave now
            if (invocationMetadataCollection == null || !invocationMetadataCollection.Any())
            {
                invocation.Proceed();
                return;
            }

            // Get the Ordering Strategy for Interceptors
            var orderingStrategy = this.proxyConfiguration.OrderingStrategy;

            // Process the BEFORE Interceptions
            foreach (var invocationContext in orderingStrategy.OrderBeforeInterception(invocationMetadataCollection))
            {
                invocationContext.Interceptor.BeforeInvoke(invocationContext);
            }

            // Execute the Real Method
            if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed))
            {
                invocation.Proceed();
            }

            // Process the AFTER Interceptions
            foreach (var invocationContext in orderingStrategy.OrderAfterInterception(invocationMetadataCollection))
            {
                invocationContext.Interceptor.AfterInvoke(invocationContext, invocationContext.GetMethodReturnValue());
            }
        }
        private Action InterceptedCallDelegate(IInvocation invocation)
        {
            var invocationContexts =
                InvocationExtensions.GetInterceptorMetadataForMethod(invocation, _serviceProvider, _configuration);

            var reversed = invocationContexts.Reverse()
                           .ToList();

            Action noActionIfBypassed = () =>
            {
                if (invocationContexts.Any(x => x.InvocationIsBypassed))
                {
                    return;
                }

                //Execute real method
                invocation.Proceed();
            };

            if (!reversed.Any())
            {
                return(noActionIfBypassed);
            }

            Action decoratedCall = () => reversed.First()
                                   .Interceptor.Invoke(reversed.First(), noActionIfBypassed);

            foreach (var c in reversed.Skip(1))
            {
                var call = decoratedCall;
                decoratedCall = () => c.Interceptor.Invoke(c, call);
            }

            return(decoratedCall);
        }
        /// <summary>
        /// Intercepts a synchronous method <paramref name="invocation"/>.
        /// </summary>
        /// <param name="invocation">The method Invocation.</param>
        public void InterceptSynchronous(IInvocation invocation)
        {
            // Map the configured interceptors to this type based on its attributes
            var invocationMetaDataCollection = InvocationExtensions.GetInterceptorMetadataForMethod(invocation, this.serviceProvider, this.proxyConfiguration);

            //If there are no configured interceptors leave now
            if (invocationMetaDataCollection == null || !invocationMetaDataCollection.Any())
            {
                invocation.Proceed();
                return;
            }

            // Get the Ordering Strategy for Interceptors
            var orderingStrategy = this.proxyConfiguration.OrderingStrategy;

            // Process the BEFORE Interceptions
            foreach (var invocationContext in orderingStrategy.OrderBeforeInterception(invocationMetaDataCollection))
            {
                invocationContext.Interceptor.BeforeInvoke(invocationContext);
            }

            Exception exToThrow = null;

            // Execute the Real method
            if (!invocationMetaDataCollection.Any(p => p.InvocationIsBypassed))
            {
                try
                {
                    invocation.Proceed();
                    exToThrow = null;
                }
                catch (Exception ex)
                {
                    exToThrow = ex;
                }
            }
            // Process the AFTER Interceptions
            ConcurrentDictionary <string, object> contextTempData = null;

            foreach (var invocationContext in orderingStrategy.OrderAfterInterception(invocationMetaDataCollection))
            {
                //Copy stores data which can be passed between interceptors
                if (contextTempData != null && contextTempData.Count > 0)
                {
                    invocationContext.CopyTempData(contextTempData);
                }
                if (!invocationContext.InvocationIsBypassed)
                {
                    invocationContext.Interceptor.AfterInvoke(invocationContext, invocationContext.GetMethodReturnValue(), exToThrow);
                    contextTempData = invocationContext.TempData;
                    exToThrow       = invocationContext.MethodException;
                }
            }

            if (exToThrow != null)
            {
                throw exToThrow;
            }
        }
        /// <summary>
        /// Intercepts an asynchronous method <paramref name="invocation"/> with return type of <see cref="Task{T}"/>.
        /// </summary>
        /// <typeparam name="TResult">The Type of the <see cref="Task{T}"/> <see cref="Task{T}.Result"/>.</typeparam>
        /// <param name="invocation"> The method invocation.</param>
        public void InterceptAsynchronous <TResult>(IInvocation invocation)
        {
            // Map the configured interceptors to this type based on its attributes
            var invocationMetaDataCollection = InvocationExtensions.GetInterceptorMetadataForMethod(invocation, this.serviceProvider, this.proxyConfiguration);

            // If there are no configured interceptors leave now
            if (invocationMetaDataCollection == null || !invocationMetaDataCollection.Any())
            {
                invocation.Proceed();
                return;
            }
            invocation.ReturnValue = InterceptAsynchronous <TResult>(invocation, invocationMetaDataCollection);
        }
Exemple #5
0
        private bool InterceptBeforeProceed(IInvocation invocation, out List <InvocationContext> invocationMetadataCollection, out IOrderingStrategy orderingStrategy)
        {
            // Map the configured interceptors to this type based on its attributes
            invocationMetadataCollection = InvocationExtensions.GetInterceptorMetadataForMethod(invocation, this.serviceProvider, this.proxyConfiguration);

            // If there are no configured interceptors, leave now
            if (invocationMetadataCollection == null || !invocationMetadataCollection.Any())
            {
                orderingStrategy = null;
                return(false);
            }

            // Get the Ordering Strategy for Interceptors
            orderingStrategy = this.proxyConfiguration.OrderingStrategy;

            // Process the BEFORE Interceptions
            foreach (var invocationContext in orderingStrategy.OrderBeforeInterception(invocationMetadataCollection))
            {
                invocationContext.Interceptor.BeforeInvoke(invocationContext);
            }

            return(true);
        }