iHttpHandler vs iHttpAsyncHandler

I have been playing with both the iHttpHandler and iHttpAsyncHandler lately, and I came across some interesting information.  There are a couple of blog posts out there explaining when to use one over the other (see Mads Kristensen's post here, or Vlad Hrybok's post here)

So when does it make sense to use Async?

Consensus seems to be that it makes sense to use the iHttpAsyncHandler for long running operations, so while it probably doesn't make sense to use an iHttpAsyncHandler for say, loading images, it probably does for having a web-service perform some [relatively] processor intensive work. In my case, I considering using the Async handler for generating XML that is fed to a flash movie.  Still, there was a potential can of worms here.  Per this MSDN article from Fritz Onion...

"There is one remaining problem with this asynchronous handler implementation—it has the potential to create an unbounded number of threads. If a large number of requests are made to the asynchronous handler, all of which take a significant amount of time to service, then you could easily end up creating more threads than the underlying operating system can handle..."

Fritz goes onto state that....

"Building asynchronous handlers to service CPU-intensive requests only adds threads to compete with the ASP.NET thread pool threads and may actually slow down the overall processing time for the request."

The moral of the story, don't use AsyncHandlers because they *sound* like they'll give you a performance boost.  Application of the Async handler should be thoughtful and empirically driven.  That said, Fritz does provide a link to a ThreadPool library that Mike Woodring developed, DeveloperMentor, (link here), should you want to take full advantage of Asynchronous capabilities in a thread-safe manner.

You'd think after all that warning I wouldn't provide the code to create/implement an iHttpAsyncHandler, but since I couldn't find VB.NET code too easily, I figured I'd provide it here.

 
Public Class MyAsyncExample
Implements IHttpAsyncHandler
' You'll need this to read from session
' Implements IReadOnlySessionState 
' You'll need this to read/write from session
' Implements IRequiresSessionState
 
    Private AsyncTask As AsyncTaskDelegate
    Protected Delegate Sub AsyncTaskDelegate(ByVal context As HttpContext)
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpAsyncHandler.ProcessRequest
    'Do what you need 
    End Sub
    
    Public Function BeginProcessRequest(ByVal context As HttpContext,ByVal callback As AsyncCallback, ByVal extraData As Object) _
        as IAsyncResult Implements IHttpAsyncHandler.BeginProcessRequest
        
        Me.AsyncTask = new AsyncTaskDelegate(Addressof ProcessRequest)
        Return AsyncTask.BeginInvoke(context,callback,extraData)
    End Function
    
    Public Sub EndProcessRequest(ByVal ar As IAsyncResult) Implements IHttpAsyncHandler.EndProcessRequest
        Me.AsyncTask.EndInvoke(ar)
    End Sub
    
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpAsyncHandler.IsReusable
        Get
            Return False
        End Get
    End Property
 
End Class

If you're looking for more information the MSDN link to Fritz's article, though far from new, is a great place to start.

Enjoy

Twitter