[导入]How can I subscribe to events exposed by a remoted object?

本文探讨了在不同应用域间实现事件触发的技术方案。详细介绍了客户端必须继承自 MarshalByRefObject 类的原因,并提出了一种优雅但较为复杂的解决方案,即通过共享组件暴露代理类的方法来实现代理调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


     Delegates require information about the type that the method is associated with in order to make a call.  In a single app-domain, this isn't a problem, because the server (the object firing the events) has access to the type information for the client (which has the event handlers) by way of the delegate.

     However, during remoting, the server most likely does not have any information about the client.  If you want events from the server to fire in the client app domain, then the client must derive fromMarshalByRefObject.  This is required so that the server will call back into the client, as opposed to a copy of the client object that is passed to the server.

     A simple way to do this is to place a copy of the client assembly in the same directory where the server directory is.  While this will work, it is not an elegant solution, as it exposes the client type unecessarily.

     A more elegant solution (albiet more complex), is to have a single assembly that is referenced by both the client and the server.  This assembly will have a shim class which will expose methods that share the signature of the events that you want to shim.  The assembly will also provide an interface with methods that share the signature of the events as well.  The shim will then take a reference to the interface, and the methods will aggregate the call to the interface that was passed in (I recommend the constructor as the place to pass the interface implementation).

     Finally, the client object will implement the interface.  When subscribing to the events that the server exposes, attach the delegates to the methods on the shim, which will be passed your client object's implementation.

     It is also important to note that in order for this to work, the TypeFilterLevel property on the sink provider for the server needs to be set to TypeFilterLevel.Full.





本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/09/22/45395.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值