Value of async tasks

Onion Blog

Syndication

I've posted a couple of entries on the new Async page and async task features of ASP.NET 2.0. One question that has been coming to mind lately is whether async tasks add any benefit for parallelizing asynchronous web service invocations?
 
Here's what I mean - you can use async tasks to issue multiple concurrent requests to web service endpoints as follows:
 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AsyncTaskVsAsyncWs.aspx.cs"
               Inherits="PS.Samples.AsyncTaskVsAsyncWs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
 
<body>
   
<form id="form1" runat="server">
     
<asp:Label runat="server" ID="resultsLabel" />
   
</form>
  </
body>
</
html>

// Code behind file
namespace PS.Samples
{
  public partial class AsyncTaskVsAsyncWs : Page
  {
    WebService1 _ws1 = new WebService1();
    WebService1 _ws2 = new WebService2();
    protected void Page_Load(object sender, EventArgs e)
    {
       PageAsyncTask task1 = new PageAsyncTask(
            delegate (object src, EventArgs ea, AsyncCallback cb, object state)
                  { return _ws1.BeginHelloWorld(cb, state); },
            delegate (object src, EventArgs ea, AsyncCallback cb, object state)
                 { resultsLabel.Text += _ws1.EndHelloWorld(cb, state); },
             null, null);
      PageAsyncTask task2 = new PageAsyncTask(
            delegate (object src, EventArgs ea, AsyncCallback cb, object state)
                { return _ws2.BeginHelloWorld2(cb, state); },
            delegate (object src, EventArgs ea, AsyncCallback cb, object state)
                { resultsLabel.Text += _ws2.EndHelloWorld2(cb, state); },
              null, null);
     RegisterAsyncTask(task1);
     RegisterAsyncTask(task2);
   }
 }
}
 
Or alternatively, you can simply invoke the async web services directly (using the same .aspx file):
 
// Code behind file
namespace PS.Samples
{
  public partial class AsyncTaskVsAsyncWs : Page
  {
    WebService1 _ws1 = new WebService1();
    WebService1 _ws2 = new WebService2();
    protected void Page_Load(object sender, EventArgs e)
    {
        IAsyncResult iar1 = _ws1.BeginHelloWorld(null, null);
        IAsyncResult iar2 = _ws2.BeginHelloWorld2(null, null);
      resultsLabel.Text += _ws1.EndHelloWorld(iar1);
      resultsLabel.Text += _ws2.EndHelloWorld2(iar2);
   }
 }
}
 
In both cases, the primary request thread is blocked while the asynchronous requests are made, and in both cases the total time taken is the longest web service call (not the sum as it would be sequentially), which is the big win. The second technique is obviously quite a bit simpler.
 
From what I can tell, the big advantage to using async tasks to do this are:
  1. You can make the page itself asynchronous by just setting the attribute in the @Page directive
  2. You can specify a timeout delegate and have a recovery plan if a call takes too long (you could do this by calling WaitOne on the IAsyncResult's AsyncWaitHandle too, but that would take some more coding.
 
Besides that, they amount to pretty much the same thing, so if you just want to call multiple concurrent web services, the second technique seems the right way to go (since it's simpler). Anyone see any other reasons I'm missing?

Posted Jun 15 2005, 10:39 AM by fritz-onion
Filed under:

Comments

Intelligent Freelance Outsourcing wrote re: Value of async tasks
on 06-15-2005 6:36 PM
Thanks Fritz, been wondering aboout this too.
Michiel van Otegem wrote re: Value of async tasks
on 06-16-2005 4:35 AM
Very appealing, but you'd have to be careful not to exhaust the ASP.NET thread pool, wouldn't you?
Fritz Onion wrote re: Value of async tasks
on 06-16-2005 4:40 AM
Actually, no, you don't conflict with the ASP.NET threadpool since it uses worker threads to process requests. Async Web Service calls use I/O threads which is a separate threadpool (also managed). It would be problematic on Windows 2000 or XP where ASP.NET uses I/O threads too.
-Fritz
K. Scott Allen wrote Async Pages In ASP.NET 2.0
on 06-16-2005 7:52 PM
K. Scott Allen wrote Async Pages In ASP.NET 2.0
on 06-16-2005 7:56 PM
David Taylor wrote re: Value of async tasks
on 06-28-2005 6:19 PM
Yes, surely the main benefit is when used in conjunction with an async page, such that you are not blocking. In your second example, when using an async page the asp.net worker thread would obviously block, while in the first example no threads would be blocked.
Kelly Anderson wrote re: Value of async tasks
on 08-03-2005 12:43 PM
Is there any way to get the primary request thread to behave Asynchronously? Long story short, I am writing an Ajax import progress indicator, that makes multiple calls to count records in a table. So you up upload a ton of records that take 2 minutes to be imported, (blocking the primary request thread on the server). The problem is that since the primary thread is blocked my update requests go unhandled until the import is done, defeating the purpose of the progress indicator.

Some Assembly Required wrote Async Pages, Async Tasks, and AsyncTimeout
on 11-04-2005 3:41 PM
Bobby wrote re: Value of async tasks
on 02-28-2007 2:11 AM
Hi Thanks for the article.

I need some info regarding the Async Pages or Async tasks.

Question is are Async Pages used only to call webservice from my Aspx Page?

If the answer for the abovve question is NO then i have one more question.


My project has a architecture like this

UI(containing aspx,html etc)--> Class Libraray(basically a facade layer which has get/set properties . This layer will call web services)--> Web services(web services call DB)


In this architecture can i call the Classess(in the second layer ) in a async way ?

Can i call the web service in a async way from the class in my class library project?

Please let me know whether its possible.

Add a Comment

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