我有一个小问题,我无法弄清楚。我有一个服务器端 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。”。谁能指出如何让它正常工作?我需要在远程对象的方法调用之前和之后注入一些代码。干杯!