简体   繁体   中英

Async Web Service Calls

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:

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

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

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

  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.

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.

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.

  • Step 0 : Create the web service.
  • Step 1 : Create the web app project (assuming it's ASP.NET).
  • Step 2 : Add a web reference to the webs service to your web app project.
  • Step 3 : The reference would create a proxy for you, using which you can invoke both synchronous and asynchronous calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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