【问题标题】:Castle DynamicProxy - Carry out deserialized invocation on different targetCastle DynamicProxy - 在不同的目标上执行反序列化调用
【发布时间】:2015-12-05 22:42:04
【问题描述】:

我正在尝试使用 DynamicProxy 生成没有目标的代理,当调用代理上的方法时,我想序列化 IInvocation 然后传递给将使用 IOC 的另一个进程(在另一台机器上)容器来解析目标类型并在其新解析的目标上执行调用。

我的调用接收器如下所示:

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
    }
}

我不确定我是否采取了正确的方法,话虽如此,库中是否有一个助手可以让我这样做?

【问题讨论】:

  • DynamicProxy 中有IInvocationReceiver 吗?还是你的意思是IInterceptor
  • 感谢@YacoubMassad,我从示例中删除了它,IInvocationReceiver 是我自己的接口

标签: c# castle castle-dynamicproxy


【解决方案1】:

你可能想做这样的事情:

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;
}

try/catch 块是可选的。我这样做是为了确保万一目标对象抛出异常,我们会抛出相同的异常,而不是包装异常的 TargetInvocationException。看看this

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2023-03-03
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多