[英]Handling dropped clients in a duplex binding WCF application
We are using a pub-sub model in our WCF application that pretty much follows the Microsoft sample: Design Patterns: List-Based Publish-Subscribe . 我们在WCF应用程序中使用了pub-sub模型,该模型几乎遵循Microsoft示例: 设计模式:基于列表的发布 - 订阅 。
Whilst the service provides a notion of subscribe()
and unsubscribe()
, what is the best practice to handle the cleanup in the situation when a client dies or the channel faults? 虽然该服务提供了
subscribe()
和unsubscribe()
的概念,但在客户端死亡或通道出现故障时处理清理的最佳做法是什么? Currently, when a client subscribes I attach to handlers to the current InstanceContext
's Closed
and Faulted
events (the service users an PerSession instance context mode and netTcpBinding): 目前,当客户端订阅时,我将处理程序附加到当前
InstanceContext
的Closed
和Faulted
事件(服务用户PerSession实例上下文模式和netTcpBinding):
_communicationObject = OperationContext.Current.InstanceContext;
_communicationObject.Closed += OnClientLost;
_communicationObject.Faulted += OnClientLost;
The OnClientLost
handler simply unsubscribes the client, however: OnClientLost
处理程序只是取消订阅客户端,但是:
This question poses a similar question but ultimately does not provide answers to the cases outside of the client calling subscribe and/or unsubscribe 这个问题提出了类似的问题,但最终没有提供客户呼叫订阅和/或取消订阅之外的案例的答案
Thanks 谢谢
I did some testing where I attached handlers to the Closed and Faulted events of the callback channel, then killed the client at the point just before the callback would be invoked by the server. 我做了一些测试,我将处理程序附加到回调通道的Closed和Faulted事件,然后在服务器调用回调之前就杀死了客户端。 On each trial, the Closed/Faulted event was fired instantaneously and before the server attempted to invoke the callback.
在每次试验中,Closed / Faulted事件在服务器尝试调用回调之前立即触发。 All the same, I still have the callback invocation wrapped in a try-catch block because the destruction of the client channel could occur just as another thread was entering the callback.
尽管如此,我仍然将回调调用包装在try-catch块中,因为客户端通道的破坏可能发生在另一个线程进入回调时。
The only clean-up necessary was to remove the reference to the callback channel. 唯一需要清理的是删除对回调通道的引用。 WCF and the garbage-collector do the rest.
WCF和垃圾收集器完成剩下的工作。
Handling those events will keep your list of subscribers synchronized. 处理这些事件将使您的订阅者列表保持同步。 It is indeed robust enough.
它确实足够强大。 Just remember that if a client drops during a transmission of a message, you might get an exception before those events fire, so be ready to ignore them so that the events can clean up.
请记住,如果客户端在传输消息期间丢失,您可能会在这些事件触发之前获得异常,因此请准备好忽略它们以便事件可以清除。
Except for removing the client from he subscribers list, additional cleanup depends entirely on your application (ie freeing resources you acquired when the client connected). 除了从客户列表中删除客户端之外,额外的清理完全取决于您的应用程序(即释放客户端连接时获得的资源)。 I am not aware of any other cleanup that is required.
我不知道需要进行任何其他清理工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.