异步Web服务调用 [英] Async Web Service Calls

查看:99
本文介绍了异步Web服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找创建使用异步Web服务调用Web服务和相应的Web应用程序。我看过很多关于如何做异步调用但没有似乎适合正是我试图做或正在使用一个真正过时技术的建议。我试图做到这一点在ASP.net 3.5(VS2008)

I'm looking to create a web service and accompanying web app that uses an async web service call. I've seen plenty of suggestions on how to do async calls but none seem to fit exactly what i'm trying to do or are using a really outdated tech. I'm trying to do this in ASP.net 3.5 (VS2008)

我需要做的是:


  1. 网页需要提交到服务
  2. 的请求
  3. 页面则需要轮询服务,每5秒左右,看是否在任务完成

  4. 一旦完成请求,需要从服务检索。

可能有人给我一些建议或点我在正确的方向?

Could someone give me some suggestions or point me in the right direction?

推荐答案

我通常都处理异步服务器端处理的方法是:

The way I have typically handled asynchronous server-side processing is by:


  1. 有网页发起针对Web服务的请求,并有服务返回一个ID为长期运行的事务。就我而言,我已经在客户端的网页和JSON格式返回数据的Web服务使用的Ajax使用jQuery。 ASP.NET MVC是特别适合于这一点,但你可以使用ASP.NET为响应返回JSON字符串为GET,或者不使用JSON的。

  1. Have the webpage initiate a request against a webservice and have the service return an ID to the long-running transaction. In my case, I have used Ajax with jQuery on the client webpage and a webservice that returns data in JSON format. ASP.NET MVC is particularly well suited for this, but you can use ASP.NET to return JSON string in response to a GET, or not use JSON at all.

有无服务器在于还存储要被处理的相关联的数据的数据库中创建记录。该事务的ID返回给客户端的网页。然后该服务通过一个消息队列发送一个消息给一个第三服务。在我的情况下,服务是在Windows服务主办了MSMQ作为中介WCF服务。应当指出的是,最好不要做实际的任务处理在ASP.NET的,因为它并不意味着是长期运行的请求。在高需求的系统,您可能耗尽可用线程。

Have the server create a record in a database that also stores the associated data to be processed. The ID of this transaction is returned to the client webpage. The service then sends a message to a third service via a message queue. In my case, the service was a WCF service hosted in a Windows Service with MSMQ as the intermediary. It should be noted that it is better not to do the actual task processing in ASP.NET, as it is not meant for requests that are long-running. In a high demand system you could exhaust available threads.

一个第三服务接收并通过从数据库中读取与处理必要的数据来响应排队消息。它最终标志着数据库记录完成。

A third service receives and responds to the queued message by reading and processing necessary data from the database. It eventually marks the database record "complete".

客户端网页轮询web服务传递交易记录ID。该web服务查询根据此ID的数据库,以确定该记录被标记为完成与否。如果它是完整的,将查询结果数据集并返回。否则,它返回一个空集。

The client webpage polls the webservice passing the transaction record ID. The webservice queries the database based on this ID to determine if the record is marked complete or not. If it is complete, it queries for the result dataset and returns it. Otherwise it returns an empty set.

客户端网页处理Web服务响应,要么包含产生的数据或空集,其中应该继续投票。

The client webpage processes the webservice response, which will either contain the resulting data or an empty set, in which it should continue polling.

这只是作为一个例子,你会发现,你可以走捷径,避免第三个服务做处理,只是使用ASP.NET线程。但是,presents它自己的问题,你会即如何有另一个请求(轮询请求)知道,如果原始请求已经完成。该hackish的解决方案到是一个静态变量,它会举行一个事务ID /结果对使用一个线程安全的集合。但对于这一努力,这真的是最好使用一个数据库。

This just serves as an example, you may find that you can take shortcuts and avoid doing processing in a third service and just use ASP.NET threads. But that presents it's own problems, namely how you would have another request (the polling request) know if the original request is complete. The hackish-solution to that is to use a thread-safe collection in a static variable which would hold a transaction ID/result pair. But for that effort, it really is better to use a database.

编辑:我现在看到,这似乎是一个示范,而不是一个生产系统。我还是坚持我的真实世界的情况下,上述纲要表态,但对于演示了hackish的解决方案就足够了。

I see now that it appears to be a demonstration rather than a production system. I still stand by my above outline for "real-world" situations, but for a demo the "hackish" solution would suffice.

这篇关于异步Web服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆