One more thought on ASP.NET Aynchronous Handlers
Nicolas Galler | February 7, 2008OK, this is going to be really obtuse to anyone who hasn’t played with IHttpAsyncHandler, but might save me a headache when I try debugging the same problem in 2 months.
Initially I had thought that an asynchronous handler would execute “out of context”… that is, the process would be:
- Create context, begin request
- Invoke “BeginProcessRequest” from the handler
- End request, destroy context (INCORRECT!)
- Later on, when the handler finishes – return the result, but do not call End request again, since we are not on the same thread anymore
Looking back, this does not make much sense – funny how things tend to look SOOO obvious once you figured them out. But for some odd reason I thought the HttpContext would not survive accross different thread and so everything
had to happen on the initial thread.
Here is what really happens:
- Create context, begin request
- Invoke “BeginProcessRequest” from the handler
- Go on and do stuff while the handler works
- Once the handler finishes, call End request, and return the result – this could happen on a different thread than the one we started on!
One more note of interest – in the “BeginRequest” event, HttpContext will be available, but context.Handler is still null. Context.Request.Path is available, though.





