如何将透明客户端代理添加到远程对象
我有一个小问题我无法弄清楚。 我有一个服务器端 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
知道了。 你的评论让我走上了正轨。 关键是解开代理并对其调用调用。 谢谢你!!!!!
Got it. your comment put me on the right track. The key is to unwrap the proxy and call invoke on it. THANK YOU!!!!!
我不久前就这样做了,但忘记了确切的过程,但尝试使用 RemotingServices.GetRealProxy 从 test 对象获取代理,并将其传递到您的 MyProxy 并调用它的调用。
像这样的事情:
您必须更新 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:
You'll have to update MyProxy class to work with RealProxy insted of direct class