调用并发 Web 服务线程
我有以下问题: 我通过 HttpWebRequest 在 Web 服务中发出多个请求,如下所示。 我正在创建所有请求的列表,然后我在列表中执行一个操作来调用每个请求。事实证明,为了运行所有内容,我的页面正在等待所有请求,然后页面被释放。我喜欢做什么? 对于每个请求,系统都会在不同的线程中发出请求,每个线程完成,将结果扔到变量中,或者列表中......根据我的需要。 有人有解决方案来帮助我吗?
非常感谢所有 StackOverflow 用户,因为他们给了我很多帮助。
米尔顿·卡马拉·戈麦斯
I have the following problem:
I am making several requests in a webservice via HttpWebRequest, as follows.
I'm creating a list of all requests, then I do one on my list is calling each request. It turns out that to run everything, my page is waiting for all requests and then the page is released. What I like to do?
For each request, the system would make a request in different threads, each thread to complete, throw the result into a variable, or list ... according to my needs.
Does anyone have a solution to help me?
Many thanks to all of StackOverflow users because they are helping me a lot.
Milton Câmara Gomes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,您想执行异步请求。已经有很多关于此的信息(例如 stackoverflow,官方 MSDN 演练 和 MSDN 博客帖子)。
It sounds to me like you want to perform an asynchronous request. There is a lot of information on this already (e.g. stackoverflow, official MSDN walkthrough and an MSDN blog posting).
通常的答案是线程。您可以在自己的线程上发出每个请求,然后在所有线程收到结果后返回,而不是运行每个单独的请求并等待响应。这使您可以重叠等待其他服务执行所花费的大量时间。
如果您使用的是 .NET 4,请尝试 TPL 的 Parallel.ForEach() 方法。它允许您指定应对集合中的每个项目执行特定操作,其功能与内置
foreach
关键字类似。听起来它应该很容易“插入”您现在正在使用的代码。此处的文档:http://msdn.microsoft。 com/en-us/library/system.threading.tasks.parallel.foreach.aspx。The usual answer is threading. Instead of running each individual request and waiting for the response, you can make each request on its own thread and then return when all the threads have received their result. This allows you to overlap a considerable amount of the time spent waiting for the other services to execute.
If you're using .NET 4, try the TPL's Parallel.ForEach() method. It allows you to specify that a particular operation should be performed on each item of a collection, with similar functionality to the built-in
foreach
keyword. It sounds like it should "drop in" pretty easily to the code you're using right now. Documentation here: http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.foreach.aspx.