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? 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):
虽然该服务提供了subscribe()和unsubscribe()的概念,但在客户端死亡或通道出现故障时处理清理的最佳做法是什么?目前,当客户端订阅时,我将处理程序附加到当前InstanceContext的Closed和Faulted事件(服务用户PerSession实例上下文模式和netTcpBinding):
_communicationObject = OperationContext.Current.InstanceContext;
_communicationObject.Closed += OnClientLost;
_communicationObject.Faulted += OnClientLost;
The OnClientLost
handler simply unsubscribes the client, however:
OnClientLost处理程序只是取消订阅客户端,但是:
- Is the above a good practice and alone robust enough to catch all situations when a client drops off the duplex communication? Or should the service just handle exceptions encountered at the point it attempts to communicate with the client and handle cleanup then?
- Aside from just unsubscribing the client call back handler, should any further cleanup be performed especially in the case of a fault?
以上是一个好的做法,并且足够强大,可以在客户端断开双工通信时捕获所有情况吗?或者服务是否应该只处理在尝试与客户端通信并处理清理时遇到的异常?
除了取消订阅客户端回调处理程序之外,是否应该执行任何进一步的清理,尤其是在出现故障的情况下?
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
2 个解决方案
#1
8
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. On each trial, the Closed/Faulted event was fired instantaneously and before the server attempted to invoke the callback. 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.
我做了一些测试,我将处理程序附加到回调通道的Closed和Faulted事件,然后在服务器调用回调之前就杀死了客户端。在每次试验中,在服务器尝试调用回调之前,立即触发Closed / Faulted事件。同样,我仍然将回调调用包装在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和垃圾收集器完成剩下的工作。
#2
2
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 (i.e. freeing resources you acquired when the client connected). I am not aware of any other cleanup that is required.
除了从客户列表中删除客户端之外,额外的清理完全取决于您的应用程序(即释放客户端连接时获得的资源)。我不知道需要进行任何其他清理工作。
#1
8
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. On each trial, the Closed/Faulted event was fired instantaneously and before the server attempted to invoke the callback. 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.
我做了一些测试,我将处理程序附加到回调通道的Closed和Faulted事件,然后在服务器调用回调之前就杀死了客户端。在每次试验中,在服务器尝试调用回调之前,立即触发Closed / Faulted事件。同样,我仍然将回调调用包装在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和垃圾收集器完成剩下的工作。
#2
2
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 (i.e. freeing resources you acquired when the client connected). I am not aware of any other cleanup that is required.
除了从客户列表中删除客户端之外,额外的清理完全取决于您的应用程序(即释放客户端连接时获得的资源)。我不知道需要进行任何其他清理工作。