Example #1
0
            public void Intercept(IInvocation invocation)
            {
                MethodMetadata metadata = Metadata.Get(invocation.Method);

                if (metadata.InvocationTarget != null)
                {
                    try
                    {
                        // currently invoked using reflection, if required we might switch to invocation using expressions
                        invocation.ReturnValue = metadata.InvocationTarget.Invoke(invocation.Proxy, invocation.Arguments);
                    }
                    catch (TargetInvocationException e)
                    {
                        throw e.InnerException;
                    }

                    return;
                }

                if (metadata.ActionMetadata.IsAsynchronous)
                {
                    if (!metadata.ActionMetadata.HasResult)
                    {
                        // async method
                        invocation.ReturnValue = Proxy.SendAsync(invocation.Method, invocation.Arguments);
                    }
                    else
                    {
                        Task <object> result = Proxy.SendAsync(invocation.Method, invocation.Arguments);

                        if (metadata.ActionMetadata.ResultType == typeof(object))
                        {
                            invocation.ReturnValue = result;
                        }
                        else
                        {
                            invocation.ReturnValue = metadata.AsyncValueProvider(result);
                        }
                    }
                }
                else
                {
                    // not async method
                    invocation.ReturnValue = Proxy.Send(invocation.Method, invocation.Arguments);
                }
            }