如何将透明客户端代理添加到远程对象

发布于 2024-07-06 19:47:50 字数 873 浏览 4 评论 0原文

我有一个小问题我无法弄清楚。 我有一个服务器端 MarshalByRefObject,我试图在客户端包装一个透明代理。 设置如下:

public class ClientProgram {
    public static void Main( string[] args ) {
        ITest test = (ITest)Activator.GetObject( typeof( ITest ), "http://127.0.0.1:8765/Test.rem" );
        test = (ITest)new MyProxy( test ).GetTransparentProxy();
        test.Foo();
    }
}

public class MyProxy : RealProxy {

    private MarshalByRefObject _object;

    public MyProxy( ITest pInstance )
        : base( pInstance.GetType() ) {
        _object = (MarshalByRefObject)pInstance;
    }

    public override IMessage Invoke( IMessage msg ) {
        return RemotingServices.ExecuteMessage( _object, (IMethodCallMessage)msg );
    }
}

问题在于,调用 RemotingServices.ExecuteMethod 时会引发异常,并显示消息“只能从对象的本机上下文调用 ExecuteMessage。”。 谁能指出如何让它正常工作? 我需要在远程对象的方法调用之前和之后注入一些代码。 干杯!

I got a little problem I can't figure out. I have a server side MarshalByRefObject that I'm trying to wrap a transparent proxy around on the client side. Here's the setup:

public class ClientProgram {
    public static void Main( string[] args ) {
        ITest test = (ITest)Activator.GetObject( typeof( ITest ), "http://127.0.0.1:8765/Test.rem" );
        test = (ITest)new MyProxy( test ).GetTransparentProxy();
        test.Foo();
    }
}

public class MyProxy : RealProxy {

    private MarshalByRefObject _object;

    public MyProxy( ITest pInstance )
        : base( pInstance.GetType() ) {
        _object = (MarshalByRefObject)pInstance;
    }

    public override IMessage Invoke( IMessage msg ) {
        return RemotingServices.ExecuteMessage( _object, (IMethodCallMessage)msg );
    }
}

The problem is that the call to RemotingServices.ExecuteMethod, an exception is thrown with the message "ExecuteMessage can be called only from the native context of the object.". Can anyone point out how to get this to work correctly? I need to inject some code before and after the method calls on remote objects. Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2024-07-13 19:47:50

知道了。 你的评论让我走上了正轨。 关键是解开代理并对其调用调用。 谢谢你!!!!!

public class ClientProgram {
        public static void Main( string[] args ) {
            ITest test = (ITest)Activator.GetObject( typeof( ITest ), "http://127.0.0.1:8765/Test.rem" );
            ITest test2 = (ITest)new MyProxy( test ).GetTransparentProxy();
            test2.Foo();
        }
    }

public class MyProxy : RealProxy {

    private object _obj;

    public MyProxy( object pObj )
        : base( typeof( ITest ) ) {
        _obj = pObj;
    }

    public override IMessage Invoke( IMessage msg ) {
        RealProxy rp = RemotingServices.GetRealProxy( _obj );
        return rp.Invoke( msg );
    }
}

Got it. your comment put me on the right track. The key is to unwrap the proxy and call invoke on it. THANK YOU!!!!!

public class ClientProgram {
        public static void Main( string[] args ) {
            ITest test = (ITest)Activator.GetObject( typeof( ITest ), "http://127.0.0.1:8765/Test.rem" );
            ITest test2 = (ITest)new MyProxy( test ).GetTransparentProxy();
            test2.Foo();
        }
    }

public class MyProxy : RealProxy {

    private object _obj;

    public MyProxy( object pObj )
        : base( typeof( ITest ) ) {
        _obj = pObj;
    }

    public override IMessage Invoke( IMessage msg ) {
        RealProxy rp = RemotingServices.GetRealProxy( _obj );
        return rp.Invoke( msg );
    }
}
冷︶言冷语的世界 2024-07-13 19:47:50

我不久前就这样做了,但忘记了确切的过程,但尝试使用 RemotingServices.GetRealProxy 从 test 对象获取代理,并将其传递到您的 MyProxy 并调用它的调用。

像这样的事情:

ITest test = (ITest)Activator.GetObject( typeof( ITest ), "http://127.0.0.1:8765/Test.rem" );
RealProxy p2 = RemotingServices.GetRealProxy(test)
test = (ITest)new MyProxy( p2 ).GetTransparentProxy();
test.Foo();

您必须更新 MyProxy 类才能与直接类中的 RealProxy 一起使用

I did that a while ago and forgot exact procedure, but try using RemotingServices.GetRealProxy to get proxy from test object and pass this into your MyProxy and call invoke on it.

Something like this:

ITest test = (ITest)Activator.GetObject( typeof( ITest ), "http://127.0.0.1:8765/Test.rem" );
RealProxy p2 = RemotingServices.GetRealProxy(test)
test = (ITest)new MyProxy( p2 ).GetTransparentProxy();
test.Foo();

You'll have to update MyProxy class to work with RealProxy insted of direct class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文