如何用C#做一个长轮询客户端?
我有一个 C# 桌面应用程序,并且使用 Web 服务没有问题(wsdl 通过“添加服务引用”添加,因此我创建一个对象并调用其函数)。
现在,我想使用长轮询技术,但我不知道如何从客户的角度做到这一点。
如何配置超时时间? 我应该使用专用于此的线程吗? 有 C# 桌面应用程序的示例吗? (没找到)
谢谢, 达姆的
I have a C# desktop application, and I consume a web service without problems (wsdl added by "add Service References", so I create an object and call its functions).
Now, I want to use long polling techniques but I can't figure how to do this from the client's perspective.
How to configure the timeout ?
Should I use a thread dedicated to this ?
Is there any example for a C# desktop application ? (haven't found any)
Thanks,
Dam's
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够配置 Web 服务对象的超时 - 详细信息具体取决于它所使用的类,但请查看
WebClientProtocol.Timeout
为例。现在,您可以从专用线程同步调用它,也可以对 Web 服务进行异步调用来开始,指定在以下情况下要执行的回调(可能在线程池线程上):服务回复。在这种情况下,您可能会发现您可以指定异步调用本身的超时 - 同样,这将取决于您拥有哪种类型的 Web 服务代理类。
这样,您就不需要“浪费”线程来等待响应 - 但您可能会发现异步编程模型比同步编程模型更难理解。如果您在任何时候只收到一两个请求,那么额外的几个线程不太可能成为问题。如果您正在等待来自 500 个不同服务的响应,那就是另一回事了,异步模型肯定是正确的选择。
You should be able to configure the timeout on the web service object - the details will depend on exactly which class it's using, but look at
WebClientProtocol.Timeout
for an example.Now you could either call that synchronously from a dedicated thread, or you could make an asynchronous call to the web service to start with, specifying a callback to be executed (probably on a thread pool thread) when the service replies. In that case, you may find you can specify the timeout on the asynchronous call itself - again, it will depend on exactly what kind of web service proxy class you've got.
That way you don't need to "waste" a thread just waiting for the response - but you may find that the asynchronous programming model is harder to understand than the synchronous one. If you've only got one or two of these requests at any one time, the extra couple of threads is unlikely to be an issue. If you're waiting for responses from 500 different services, that's a different matter and the async model would definitely be the way to go.
对于线程问题,请参阅 Jon 的回答。
对于超时问题,解决方法如下:
在 vs 2008 中,当我从 wsdl 添加“服务引用”时,它将默认使用 WCF,并且我找不到如何使用它设置超时值。
因此,当右键单击“服务引用”时,我必须选择“Web 引用”(高级/添加 Web 引用)。这样,它将仅使用“正常”Web 服务,并且超时参数可用。
For threading issues, see Jon's answer.
For the timeout problem, here's the solution:
In vs 2008, when I add a "service reference" from a wsdl, it will use WCF by default, and I can't find how to set a timeout value with it.
So, when right clicking on Service References, I have to choose "web references" (advanced / add web reference). That way, it will use only "normal" web services and the Timeout parameter is available.