检查wcf客户端连接
我正在使用 WCF 双工回调创建客户端服务应用程序。该服务向所有连接的客户端发送消息。我将所有连接的客户端存储在字典中。在向客户端发送消息之前,我想检查是否有客户端异常关闭(网络断开、断电)。
@marc_s:我创建了一个 CheckCallbackChannels 方法,它将检查字典中已连接客户端的通信状态。
Sub CheckCallbackChannel()
For Each objClient As KeyValuePair(Of Guid, IClientCallBack) In MainService.objClients
Dim objClientCallBack As IClientCallBack = MainService.objClients.Item(objClient.Key)
Dim callbackChannel As ICommunicationObject = TryCast(mobjClientCallBack,ICommunicationObject)
'Dim state As CommunicationState = callbackChannel.State
If callbackChannel.State = CommunicationState.Opened OrElse callbackChannel.State = CommunicationState.Faulted Then
MainService.objClients.Remove(objClient.Key)
End If
Next
End Sub
但在这里我也得到了错误客户端的连接状态。
I am creating a client service application using WCF duplex callback. The service sends messages to all connected clients. I am storing all connected clients in the dictionary. Before sending message to the client I want to check if any client has shutdown abnormally (network disconnection, power off).
@marc_s: I created a CheckCallbackChannels method which will check for Communication State of a connected clients in the dictionary.
Sub CheckCallbackChannel()
For Each objClient As KeyValuePair(Of Guid, IClientCallBack) In MainService.objClients
Dim objClientCallBack As IClientCallBack = MainService.objClients.Item(objClient.Key)
Dim callbackChannel As ICommunicationObject = TryCast(mobjClientCallBack,ICommunicationObject)
'Dim state As CommunicationState = callbackChannel.State
If callbackChannel.State = CommunicationState.Opened OrElse callbackChannel.State = CommunicationState.Faulted Then
MainService.objClients.Remove(objClient.Key)
End If
Next
End Sub
but here i get the state as connected for faulty clients also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以在每个服务合约中包含一个
Ping()
方法,并在实际调用之前调用该方法。但这真正告诉你什么?
首先,即使调用
.Ping()
也可能导致异常(因为服务消失、过载等)。如果它成功了 - 它绝对不能保证你下一次调用同一服务(一微秒后)有成功的机会......
基本上,我发现这种方法很浪费 - 你需要写了很多代码,到最后,它确实没有给你任何有意义的信息。
您需要做的是:为每个服务调用做好准备,因为无论出于何种原因,它都可能失败。不要浪费时间和处理周期在整个系统中进行 ping 操作 - 无论如何它不会为您提供任何有意义的信息。
只需进行服务调用,准备好处理故障(并可能回收您的 WCF 代理) - 这就是您真正能做的。
Yes, you could include a
Ping()
method on each service contract, and call that before your actual call.But what does that really tell you??
First of all, even the call to
.Ping()
could result in an exception (since the service is gone, overloaded, whatever).And if it succeeds - it gives you absolutely no guarantee that your next call to that same service, a microsecond later, has any chance of succeeding....
Basically, I find this approach wasteful - you need to write a lot of code, and in the end, it really doesn't give you any meaningful information.
What you need to do is: be prepared on every service call that it can fail - for whatever reason. Don't waste your time and processing cycles pinging around all over your system - it doesn't give you any meaningful info anyway.
Just make your service calls, be prepared to handle failures (and possibly recycle your WCF proxies) - and that's all you can really do.