Async tasks in ASP.NET 2.0

Onion Blog

Syndication

In an earlier post, I looked at the new async page feature in ASP.NET 2.0. Several people asked the very good question - what if I wanted to make multiple asynchronous Web service calls instead of just one (which is what my sample showed)? Fortunately, they thought of this scenario too, and in addition to asynchronous pages ASP.NET 2.0 they give us asynchronous tasks as well (thanks to David Taylor for initially pointing me to this new feature and to Erik Olson on the ASP.NET team for discussing some of the details of implementation).
 
To support multiple, concurrent calls to an asynchronous method, the new Page class provides a RegisterAsyncTask() method that takes an instance of a class called PageAsyncTask. This class consists of three delegates (a start delegate, an end delegate, and a timeout delegate) along with some other state. To use the async task feature, you call RegisterAsyncTask() (typically from your Load event handler) and ASP.NET will take care to issue each task that you register in parallel and take care of synchronizing the results for you. There is also a new top-level Page attribute, asyncTimeout, that defines the maximum number of seconds you will wait for any of the asynchronous tasks to complete before timing out. Note that this feature is independent of the asynchronous page feature, although the two can be used together for a pretty compelling asynchronous story.
 
Without further ado, here's an example of making three outbound asynchronous calls to a Web service called 'Slow', that simply sleeps for 3 seconds and returns. When run serially, this obviously takes a little more than 9 seconds to complete. However with asynchronous tasks, all three Web service calls come back in just over 3 seconds in true parallel fashion.
 
public partial class SlowMultiAsync : Page
{
  Slow slowWebservice = new Slow();
  protected void Page_Load(object sender, EventArgs e)
  {
    PageAsyncTask task1 =
      new PageAsyncTask(
      new BeginEventHandler(BeginGetAsyncData),
      new EndEventHandler(EndGetAsyncData),
      new EndEventHandler(TimeoutGetAsyncData),
      "task1", //this is supplemental state (an object parameter) in which you can pass anything
      true); //this is whether you want to run in parallel or serially
    PageAsyncTask task2 =
      new PageAsyncTask(
      new BeginEventHandler(BeginGetAsyncData),
      new EndEventHandler(EndGetAsyncData),
      new EndEventHandler(TimeoutGetAsyncData),
      "task2",
      true);
    PageAsyncTask task3 =
      new PageAsyncTask(
      new BeginEventHandler(BeginGetAsyncData),
      new EndEventHandler(EndGetAsyncData),
      new EndEventHandler(TimeoutGetAsyncData),
      "task3",
      true);
    RegisterAsyncTask(task1);
    RegisterAsyncTask(task2);
    RegisterAsyncTask(task3);
  }
 
  IAsyncResult BeginGetAsyncData(Object src, EventArgs args,
                                                           AsyncCallback cb, Object state)
  {
    return slowWebservice.BeginHelloWorld(cb, state);
  }
 
  void TimeoutGetAsyncData(IAsyncResult ar)
  {
    Response.Write("<strong>async get data timed out</strong><br />");
  }
 
  void EndGetAsyncData(IAsyncResult ar)
  {
    string ret = slowWebservice.EndHelloWorld(ar);
    // use result to prepare response here...
  }
  //...
}
 
Again, note that you can combine this with the async page feature I discussed in a previous post, and not only will the tasks be issued concurrently, but they will not block a request thread while doing so - the best of both worlds in this case.
 
I'll follow up with more details of the implementation and some test results in the future, but I thought I'd share this now for those of you wondering if there would be support for concurrent tasks.

Posted Feb 14 2005, 06:31 AM by fritz-onion
Filed under:

Comments

Sahil Malik wrote re: Async tasks in ASP.NET 2.0
on 02-14-2005 4:39 AM
Fritz Onion is the coolest guy around.
Luis Abreu wrote re: Async tasks in ASP.NET 2.0
on 02-14-2005 2:40 PM
cool stuff!! just what i wanted to know. Now i'll just have to wait for beta 2 to run those tests :) Keep up the good work!!
Carlo Folini wrote re: Async tasks in ASP.NET 2.0
on 02-15-2005 1:14 AM
I'm investigating the async page model.
I successfully set up an async call with AddOnPreRenderCompleteAsync.
Suppose now that I have to call another ws(B) depending on the result returned from the first call (A).
Say if the result of A equals 0 I have to go on with the rendering phase, if result is 1 I need another call B and the do the rendering.
I didn't get how can I register the async call to B on the EndEventHandler of A.
Is it possible to register a PageAsyncTask in the EndEventHandler? (with AddOnPreRenderCompleteAsync it wasn't)
Kjetil Kristoffer Solberg wrote How can this work?
on 03-03-2005 4:02 AM
The thread that APS.NET uses to service the request will after OnLoad call the other OnPreRender, Render methods etc. What happens if the thread that serviced the request finsihed, naturally or via Response.End. What then happens to these async methods? How can you then call Response.Write(...) in your callback routine?
Ghost wrote re: Async tasks in ASP.NET 2.0
on 03-08-2005 2:19 PM
Does this work with the Decembet CTP version of VS.NET? Or is it only for the next beta version that is not released yet?

Please advise. Thanks!
Fritz Onion wrote re: Async tasks in ASP.NET 2.0
on 03-09-2005 4:57 AM
I'm not sure whether they're in the December CTP or not - the bits I was working with were later than that (they're definitely in beta 2).
Onion Blog wrote Value of async tasks
on 06-15-2005 8:41 AM
Some Assembly Required wrote Async Pages, Async Tasks, and AsyncTimeout
on 11-04-2005 3:41 PM
Some Assembly Required wrote Async Pages, Async Tasks, and AsyncTimeout
on 11-05-2005 8:35 AM
Shaun Ricci wrote re: Async tasks in ASP.NET 2.0
on 11-05-2005 10:29 AM
Has anyone had luck running more than 10 Async calls like this? I seem to be having issues with calling a lot.
David wrote re: Async tasks in ASP.NET 2.0
on 03-02-2006 1:31 PM
I had to pass a GUID in for UserState in order to get multiple calls working. Otherwise I received an error like:
"There was an error during asynchronous processing. Unique state object is required for multiple asynchronous simultaneous operations to be outstanding."

See: http://www.xamlon.com/forums/Topic2293-14-1.aspx#bm2297
David wrote re: Async tasks in ASP.NET 2.0
on 03-02-2006 1:32 PM
See also:

http://msdn2.microsoft.com/en-us/library/ms228974.aspx
Brett wrote re: Async tasks in ASP.NET 2.0
on 03-26-2006 4:33 AM
I want to do the following from a Windows Service. I will appreciate it if i can get guidance on how to go about doing this in the best way.

I have a web service that processes data based on a customer id. (It processes some records and sends emails). What i want to do from the windows service, is to call the web service for each of the cutomer id`s that i retreive from the database. I want to be able to procesess about 5 customer id`s at a time.

Is the above example the correct solution for what i am trying to do..?
Akshay. wrote Non-Webservice Tasks.
on 07-28-2006 12:29 PM
Thanks Fritz was such cool tutorials...
I wanted to do something similar.. Only difference being the Tasks are not webservices but a series of middle-tier calls..

I saw this link http://msdn2.microsoft.com/en-us/library/ms228974.aspx

Basically it uses BeginInvoke to perform the tasks... However I am not sure if I were to use BeginInvoke would it be from the same ASP.NET thread pool - or is it from system wide IO thread pool...

Any idea/ thoughts approceiated.
John wrote re: Async tasks in ASP.NET 2.0
on 03-14-2007 3:39 PM
Hey Fritz,

I have a scenario where I want to be able to make a large number of asynchronous web calls from one web service to another. What do you recommend is the best method of doing this ?

I have been having problems at a certain threshold, and I have not been able to determine what that is.

For example, if webservice A calls web service b, 100 times for each request to service A, is that something which is even possible, or a viable approach ?
james wrote re: Async tasks in ASP.NET 2.0
on 04-17-2007 6:13 AM
Hi - Thanks for the info - I am struggling with the multiple async calls to the ebay api and am trying to find out why it is complaining about missing UserStat - cheers
David McCarter wrote re: Async tasks in ASP.NET 2.0
on 08-16-2007 12:50 PM
This is great for ASP.NET, but how does it work for Win Forms and Web Services?

David
Refky Wahib wrote re: Async tasks in ASP.NET 2.0
on 04-29-2008 2:17 PM
I always have this error

There was an error during asynchronous processing. Unique state object is required for multiple asynchronous simultaneous operations to be outstanding.
Ranjit Singh wrote re: Async tasks in ASP.NET 2.0
on 10-13-2008 10:43 PM

Nice Example.

Working Fine With a webservice and asp.net application

Many Thanks. Keep Going. Great

Asynchronous Paging questions | keyongtech wrote Asynchronous Paging questions | keyongtech
on 01-18-2009 10:00 AM

Pingback from  Asynchronous Paging questions | keyongtech

Add a Comment

(required)  
(optional)
(required)  
Remember Me?