1

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?

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
hover
  • 789
  • 6
  • 15
  • Wouldn't it be better if the service triggers an event when completed and you could subscribe to the Asyc service call completed ? Is there any specific reason you're polling every 5 seconds ? – abhilash May 27 '11 at 03:01
  • No, it just needs some way to determine if it has finished or not. Could a page load be triggered when finished? – hover May 27 '11 at 03:06
  • 1
    Do be careful about avoiding "using a really outdated tech" just for the sake of avoiding something that's not new. Sometimes the simplest, best tool for a job is one that's been around a very long time. – Dave Ward May 27 '11 at 03:34

3 Answers3

1

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.

emfurry
  • 2,158
  • 2
  • 14
  • 12
0

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.

Community
  • 1
  • 1
selion
  • 335
  • 1
  • 3
  • 6
  • I need the webpage to issue the request then 'forget' about the request and carry on with whatever else it's going to do. Then it needs to be notified somehow that the request has completed. I realize that a synchronous call may be suitable but i've been tasked with creating both synchronous and asynchronous requests. (for a jbo interview test) – hover May 27 '11 at 03:09
  • Yes, exactly "the request then 'forget' about the request and carry on with whatever". This is the normal behavior a simple websrevice will do and read up on 'jquery ajax' in google. – selion May 27 '11 at 03:12
  • 1
    looks like we would need a _job-interview-test_ tag like the _homework_ tag :) – abhilash May 27 '11 at 03:14
  • Say i put the request in my page_load or a button click event. Using a synchronous request no further code will execute until i receive a response from the service. What i'm trying to do is submit the request then be notified later on that it has finished. – hover May 27 '11 at 03:19
  • @hover have done any of the things @AB Kolan outlined ? If not I think you may not have enough time to grasp it all until the end of your test (which I suspect is a couple of days). Quick help here:http://forums.asp.net/t/1182061.aspx/1 & good luck – selion May 27 '11 at 03:19
  • I have created the service and the web page. So far the service is working synchronously but i'm trying to get the second asynchronous part working. – hover May 27 '11 at 03:21
0
  • 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.
abhilash
  • 5,605
  • 3
  • 36
  • 59