Example #1
0
        public object Invoke(object target,
                             MethodInfo method,
                             object[] arguments,
                             InvokeDelegate @delegate)
        {
            HandlerPipeline  pipeline   = GetPipeline(method, target);
            MethodInvocation invocation = new MethodInvocation(target, method, arguments);

            IMethodReturn result =
                pipeline.Invoke(invocation,
                                delegate
            {
                try
                {
                    object returnValue = @delegate(arguments);
                    return(new MethodReturn(invocation.Arguments, method.GetParameters(), returnValue));
                }
                catch (Exception ex)
                {
                    return(new MethodReturn(ex, method.GetParameters()));
                }
            });

            if (result.Exception != null)
            {
                FieldInfo remoteStackTraceString = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.Instance | BindingFlags.NonPublic);
                remoteStackTraceString.SetValue(result.Exception, result.Exception.StackTrace + Environment.NewLine);
                throw result.Exception;
            }

            return(result.ReturnValue);
        }
        public void OneHandler()
        {
            Recorder.Records.Clear();
            RecordingHandler     handler    = new RecordingHandler();
            StubMethodInvocation invocation = new StubMethodInvocation();
            HandlerPipeline      pipeline   = new HandlerPipeline(handler);

            pipeline.Invoke(invocation, delegate
            {
                Recorder.Records.Add("method");
                return(null);
            });

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }
        public void NoHandlersCallsTarget()
        {
            bool called = false;
            StubMethodInvocation invocation  = new StubMethodInvocation();
            StubMethodReturn     returnValue = new StubMethodReturn();
            HandlerPipeline      pipeline    = new HandlerPipeline();

            IMethodReturn result = pipeline.Invoke(invocation, delegate(IMethodInvocation call,
                                                                        GetNextHandlerDelegate getNext)
            {
                Assert.Same(call, invocation);
                Assert.Null(getNext);

                called = true;
                return(returnValue);
            });

            Assert.True(called);
            Assert.Same(returnValue, result);
        }
        public void TwoHandlers()
        {
            Recorder.Records.Clear();
            RecordingHandler     handler1   = new RecordingHandler("1");
            RecordingHandler     handler2   = new RecordingHandler("2");
            StubMethodInvocation invocation = new StubMethodInvocation();
            HandlerPipeline      pipeline   = new HandlerPipeline(handler1, handler2);

            pipeline.Invoke(invocation, delegate
            {
                Recorder.Records.Add("method");
                return(null);
            });

            Assert.Equal(5, Recorder.Records.Count);
            Assert.Equal("Before Method (1)", Recorder.Records[0]);
            Assert.Equal("Before Method (2)", Recorder.Records[1]);
            Assert.Equal("method", Recorder.Records[2]);
            Assert.Equal("After Method (2)", Recorder.Records[3]);
            Assert.Equal("After Method (1)", Recorder.Records[4]);
        }
Example #5
0
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage callMessage = (IMethodCallMessage)msg;
            HandlerPipeline    pipeline;

            if (handlers.ContainsKey(callMessage.MethodBase))
            {
                pipeline = handlers[callMessage.MethodBase];
            }
            else
            {
                pipeline = new HandlerPipeline();
            }

            MethodInvocation invocation = new MethodInvocation(target, callMessage.MethodBase, callMessage.Args);

            IMethodReturn result =
                pipeline.Invoke(invocation,
                                delegate
            {
                try
                {
                    object returnValue = callMessage.MethodBase.Invoke(target, invocation.Arguments);
                    return(new MethodReturn(invocation.Arguments, callMessage.MethodBase.GetParameters(), returnValue));
                }
                catch (TargetInvocationException ex)
                {
                    return(new MethodReturn(ex.InnerException, callMessage.MethodBase.GetParameters()));
                }
            });

            if (result.Exception == null)
            {
                return(new ReturnMessage(result.ReturnValue, invocation.Arguments, invocation.Arguments.Length,
                                         callMessage.LogicalCallContext, callMessage));
            }

            return(new ReturnMessage(result.Exception, callMessage));
        }