0

I am trying to use DynamicProxy to generate a proxy without a target, when a method on the proxy is invoked, I would like to serialize the IInvocation to then be passed to another process (on another machine) that will use an IOC container to resolve the target type and carry out the invocation on its new resolved target.

My invocation receiver would look something like the following:

public class InvocationReceiver
{
    private readonly UnityContainer _container;

    public InvocationReceiver(UnityContainer container)
    {
        _container = container;
    }

    public void ReceiveInvocation(Castle.DynamicProxy.IInvocation invocation)
    {
        var target = _container.Resolve(invocation.TargetType);

        Invoke(invocation, target); //Something like this
    }
}

I'm not sure if I am taking the right approach, that being said, is there a helper in the lib that would allow me to do this?

jimmyjambles
  • 1,631
  • 2
  • 20
  • 34

1 Answers1

0

You might want to do something like this:

var method_to_call = invocation.Method;

try
{
    invocation.ReturnValue = method_to_call.Invoke(target, invocation.Arguments);
}
catch (TargetInvocationException target_invocation_exception)
{
    throw target_invocation_exception.InnerException;
}

The try/catch block is optional. I put it to make sure that in case that the target object throws an exception, we throw the same exception and not a TargetInvocationException wrapping the exception. Take a look at this.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62