异步 Web 服务调用
我正在寻找创建一个使用异步 Web 服务调用的 Web 服务和随附的 Web 应用程序。我已经看到很多关于如何进行异步调用的建议,但似乎没有一个完全适合我正在尝试做的事情或正在使用真正过时的技术。我正在尝试在 ASP.net 3.5 (VS2008) 中执行此操作,
我需要做的是:
- 网页需要向服务提交请求
- ,然后页面需要每 5 秒左右轮询一次服务以查看任务是否完成 一旦完成
- ,就需要从服务中检索请求。
有人可以给我一些建议或指出我正确的方向吗?
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)
What i need to do is:
- the webpage needs to submit a request to the service
- the page then needs to poll the service every 5 seconds or so to see if the task has completed
- once complete the request needs to be retrieved from the service.
Could someone give me some suggestions or point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常处理异步服务器端处理的方式是:
让网页针对 Web 服务发起请求,并让服务向长时间运行的事务返回一个 ID。就我而言,我在客户端网页上使用了 Ajax 和 jQuery,以及返回 JSON 格式数据的 Web 服务。 ASP.NET MVC 特别适合这种情况,但您可以使用 ASP.NET 返回 JSON 字符串来响应 GET,或者根本不使用 JSON。
让服务器在数据库中创建一条记录,该记录还存储要处理的关联数据。该交易的ID返回到客户端网页。然后该服务通过消息队列向第三个服务发送消息。就我而言,该服务是托管在 Windows 服务中的 WCF 服务,并以 MSMQ 作为中介。应该注意的是,最好不要在 ASP.NET 中进行实际的任务处理,因为它不适用于长时间运行的请求。在高需求系统中,您可能会耗尽可用线程。
第三个服务通过从数据库读取和处理必要的数据来接收和响应排队的消息。它最终将数据库记录标记为“完整”。
客户端网页轮询传递交易记录 ID 的 Web 服务。 Web 服务根据此 ID 查询数据库以确定记录是否标记为完整。如果完整,它将查询结果数据集并返回它。否则返回一个空集。
客户端网页处理 Web 服务响应,该响应将包含结果数据或空集,其中应继续轮询。
这只是一个例子,您可能会发现可以采取捷径并避免在第三方服务中进行处理,而只使用 ASP.NET 线程。但这也带来了它自己的问题,即如何让另一个请求(轮询请求)知道原始请求是否完成。对此的黑客解决方案是在静态变量中使用线程安全集合,该变量将保存事务 ID/结果对。但对于这项工作,使用数据库确实更好。
编辑:我现在看到它似乎是一个演示而不是一个生产系统。对于“现实世界”情况,我仍然坚持上面的概述,但对于演示来说,“hackish”解决方案就足够了。
The way I have typically handled asynchronous server-side processing is by:
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.
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".
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.
The client webpage processes the webservice response, which will either contain the resulting data or an empty set, in which it should continue polling.
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.
EDIT: 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.
哪一部分需要进行异步?据我所知,你的动作是同步的:
1) ->; 2)-> 3)
一个简单的Web服务就可以了,IIS(就像任何Web服务器一样)支持异步处理多个请求,所以你没有问题。
您可能需要的东西知道。 JavaScript 引擎也在单个线程中执行代码。
Which part are going to need to do async ? As far as I can tell your actions are synchronous:
1) -> 2) -> 3)
A simple web service would do, IIS (as any web server) supports multiple request to be handled async so you have no problem.
Something which you may need to be aware of. And also the javascript engine executes code in a single thread.