How to do a long polling client in C#?

前端 未结 2 2003
暖寄归人
暖寄归人 2021-01-06 13:25

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).

相关标签:
2条回答
  • 2021-01-06 13:29

    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.

    0 讨论(0)
  • 2021-01-06 13:54

    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.

    0 讨论(0)
提交回复
热议问题