在 WCF 服务中的异步方法调用期间检测连接失败
我有一个异步 WCF 服务实现,我想检测客户端在 WCF 方法调用期间何时中止连接。
基本上,这就是我想要的:
- 客户端调用 WCF 方法
- 服务器开始异步处理请求
- 客户端中止连接(或由于任何原因连接失败)
- 应通知服务器客户端中止连接
这可能吗?
这是为了长轮询的实现。或者,服务器能否确保客户端成功收到响应?如果没有,则应保存响应以供下一个轮询请求使用。
I have an async WCF service implementation and I want to detect when the client aborts the connection during a WCF method call.
Basically, this is what I want:
- Client calls WCF method
- Server begins processing the request asynchronously
- Client aborts the connection (or the connection fails for whatever reason)
- Server should be notified that the client aborted the connection
Is this possible?
This is for a long-polling implementation. Alternatively, can the server make sure that the client received the response successfully? If it didn't, the response should be saved for the next poll request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道有什么可靠的方法可以知道客户端是否已连接。请参阅 这个,这个 和这个< /a>
建议的一些替代方案是使用双工通道或使用队列。基于队列的解决方案将使服务器将响应发布到队列,并让客户端从队列中选取这些响应。队列可以在MSMQ、DB、Azure中实现。
谢谢
I don't know any reliable way of knowing that client is connected or not. See this,this and this
Some of the alternative suggested are to use Duplex channels or use Queues. A Queue based solution would have server posting response to queue and client picking those responses from the queue. The queue can be implemented in MSMQ, DB, Azure.
Thanks
您可以设置一个轮询解决方案,而不是让客户端保持连接等待答案,而是发出请求并立即断开连接。然后,它每隔 X 秒重新连接一次,以轮询请求是否已完成。
在服务器上,如果您在 X * 2 秒内没有看到客户端轮询,则客户端发生了某些情况,您应该中止请求。
不幸的是,要实现这一目标,服务器上需要一些额外的基础设施来跟踪哪些请求处于活动状态以及您最后一次看到客户端的时间。
You could set up a polling solution where instead of the client staying connected to wait for an answer, it makes the request and immediately disconnects. It then reconnects every X seconds to poll if the request is completed or not yet.
On the server, if you don't see the client poll for X * 2 seconds, then something happened to the client and you should abort the request.
Making that work is going to require some extra infrastructure on the server to keep track of which requests are active and when you last saw the client, unfortunately.