简体   繁体   English

异步 Web 服务调用

[英]Async Web Service Calls

I'm looking to create a web service and accompanying web app that uses an async web service call.我正在寻找创建 web 服务和随附的 web 应用程序,该应用程序使用异步 web 服务调用。 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)我正在尝试在 ASP.net 3.5 (VS2008) 中执行此操作

What i need to do is:我需要做的是:

  1. the webpage needs to submit a request to the service网页需要向服务提交请求
  2. the page then needs to poll the service every 5 seconds or so to see if the task has completed然后页面需要每 5 秒左右轮询一次服务以查看任务是否已完成
  3. once complete the request needs to be retrieved from the service.完成后,需要从服务中检索请求。

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. Have the webpage initiate a request against a webservice and have the service return an ID to the long-running transaction.让网页向 Web 服务发起请求,并让服务向长期运行的事务返回一个 ID。 In my case, I have used Ajax with jQuery on the client webpage and a webservice that returns data in JSON format.就我而言,我在客户端网页上使用了 Ajax 和 jQuery 以及一个以 JSON 格式返回数据的 Web 服务。 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. 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.

  2. 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.此交易的 ID 返回到客户端网页。 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.在我的例子中,该服务是一个 WCF 服务,托管在一个 Windows 服务中,MSMQ 作为中介。 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.需要注意的是,最好不要在 ASP.NET 中进行实际的任务处理,因为它不适用于长时间运行的请求。 In a high demand system you could exhaust available threads.在高需求系统中,您可能会耗尽可用线程。

  3. 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".它最终将数据库记录标记为“完成”。

  4. The client webpage polls the webservice passing the transaction record ID.客户端网页轮询传递事务记录 ID 的 Web 服务。 The webservice queries the database based on this ID to determine if the record is marked complete or not. Web 服务根据此 ID 查询数据库以确定记录是否标记为完整。 If it is complete, it queries for the result dataset and returns it.如果完成,则查询结果数据集并返回。 Otherwise it returns an empty set.否则它返回一个空集。

  5. The client webpage processes the webservice response, which will either contain the resulting data or an empty set, in which it should continue polling.客户端网页处理 web 服务响应,该响应将包含结果数据或空集,它应该继续轮询。

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.这只是一个例子,你可能会发现你可以走捷径,避免在第三个服务中进行处理,只使用 ASP.NET 线程。 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. hackish 的解决方案是在 static 变量中使用线程安全集合,该变量将保存事务 ID/结果对。 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.对于“真实世界”的情况,我仍然坚持上述大纲,但对于演示,“hackish”解决方案就足够了。

Which part are going to need to do async?哪一部分需要做异步? As far as I can tell your actions are synchronous: 1) -> 2) -> 3)据我所知,您的操作是同步的: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.一个简单的 web 服务就可以了,IIS(与任何 web 服务器一样)支持异步处理多个请求,所以你没有问题。

Something which you may need to be aware of.您可能需要注意的事情 And also the javascript engine executes code in a single thread. javascript 引擎也在单线程中执行代码。

  • Step 0 : Create the web service.步骤 0 :创建 web 服务。
  • Step 1 : Create the web app project (assuming it's ASP.NET).第 1 步:创建 web 应用程序项目(假设它是 ASP.NET)。
  • Step 2 : Add a web reference to the webs service to your web app project.第 2 步:将 web 对 Web 服务的引用添加到您的 web 应用程序项目中。
  • Step 3 : The reference would create a proxy for you, using which you can invoke both synchronous and asynchronous calls.第 3 步:该引用将为您创建一个代理,您可以使用它来调用同步和异步调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM