Example #1
0
        public override IMessage Invoke(IMessage msg)
        {
            Guard.ArgumentNotNull(msg, "msg");

            IMethodCallMessage callMessage = (IMethodCallMessage)msg;

            if (callMessage.MethodBase.DeclaringType == typeof(IInterceptingProxy))
            {
                return(HandleInterceptingProxyMethod(callMessage));
            }

            HandlerPipeline pipeline = GetPipeline(TranslateInterfaceMethod(callMessage.MethodBase));

            TransparentProxyMethodInvocation invocation = new TransparentProxyMethodInvocation(callMessage, target);
            IMethodReturn result =
                pipeline.Invoke(
                    invocation,
                    delegate(IMethodInvocation input, GetNextHandlerDelegate getNext)
            {
                try
                {
                    object returnValue = callMessage.MethodBase.Invoke(target, invocation.Arguments);
                    return(input.CreateMethodReturn(returnValue, invocation.Arguments));
                }
                catch (TargetInvocationException ex)
                {
                    // The outer exception will always be a reflection exception; we want the inner, which is
                    // the underlying exception.
                    return(input.CreateExceptionMethodReturn(ex.InnerException));
                }
            });

            return(((TransparentProxyMethodReturn)result).ToMethodReturnMessage());
        }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Guard.ArgumentNotNull(input, "input");
            Guard.ArgumentNotNull(getNext, "getNext");

            HandlerPipeline pipeline = GetPipeline(input.MethodBase);

            return(pipeline.Invoke(
                       input,
                       delegate(IMethodInvocation policyInjectionInput, GetNextHandlerDelegate policyInjectionInputGetNext)
            {
                try
                {
                    return getNext()(policyInjectionInput, getNext);
                }
                catch (TargetInvocationException ex)
                {
                    // The outer exception will always be a reflection exception; we want the inner, which is
                    // the underlying exception.
                    return policyInjectionInput.CreateExceptionMethodReturn(ex.InnerException);
                }
            }));
        }