locked
How does ASP.NET async pages work with the async CTP (without expilictly calling AddOnPreRenderCompleteAsync)? RRS feed

  • Question

  • Hello,

    I was just wondering how does async pages still work without having to call the Page.AddOnPreRenderCompleteAsync() or Page.RegisterAsyncTask() methods?

    After all, there has to be some mechanism for the page to signal ASP.Net that it should suspend rendering the page until the async operation is completed, so how does the framework "knows" that (without no one to tell it)?

    I believe that by understanding this I could write better code and also be able to write async handlers using the same technique.

     

    Thanks, Yaron

    Thursday, January 20, 2011 1:16 PM

Answers

  • Hi Yaron-

    Here's a response from some folks on the ASP.NET team:

    "For the moment, the IHttpAsyncHandler is still required since we need to set up the execution pipeline before the handler is run, so we need to know ahead of time whether the handler will be synchronous or asynchronous.  Right now the best way for us to detect this is to look at the interfaces implemented by the handler type.  However, we’re open to the idea of using a different interface or base class for Task-based asynchronous handlers.  This would allow you to write a method with the signature Task ProcessRequestAsync(HttpContext context) rather than having to use the current BeginProcessRequest / EndProcessRequest pattern."

    Thanks.

     

     

     

    Friday, January 28, 2011 1:50 AM
    Moderator

All replies

  • Hi Yaron-

    ASP.NET has another mechanism besides the ones you mention for hooking in with asynchronous operations.  The two you mention explicitly register an asynchronous operation with the page, but there's an implicit mechanism as well: SynchronizationContext.  ASP.NET implements and publishes a SynchronizationContext as SynchronizationContext.Current when running in an ASP.NET app, and it overrides the SynchronizationContext's OperationStarted and OperationEnded methods to maintain a counter of how many outstanding operations exist, and the page waits to complete until all operations have completed.  When you write a void-returning asynchronous method, since there's no task returned from the async method with which to track the async method, we instead (and largely for ASP.NET) hook into the current SynchronizationContext if one exists and call the relevant OperationStarted and OperationEnded methods.  That way, you can uses awaits inside of your various ASP.NET event handlers, like Page_Load, and ASP.NET will be able to track when your asynchronous methods have completed.

    Thursday, January 20, 2011 4:03 PM
    Moderator
  • Hi Stephen,

    Thank you for the reply, I suspected it has something to do with the synchronization context.

    So what about HTTP Handlers? usually, in order to achive asynchronous operation, you would need to implement IHttpAsyncHandler (which the async page internally does for you), but with this implicit use of the synchronization context, it seems that it is possible to implement async handlers that only implement IHttpHandle and uses OperationStarted at the begining of the processing and OperationEnded at the very end. Is this true? Is it enough to add the 'async' keyword to the IHttpHandle 's ProcessRequest method, use awaits in it and it will become async? Is the ASP.NET manages this case also?

    Thanks, Yaron

    Sunday, January 23, 2011 12:13 PM
  • Hi Yaron-

    Here's a response from some folks on the ASP.NET team:

    "For the moment, the IHttpAsyncHandler is still required since we need to set up the execution pipeline before the handler is run, so we need to know ahead of time whether the handler will be synchronous or asynchronous.  Right now the best way for us to detect this is to look at the interfaces implemented by the handler type.  However, we’re open to the idea of using a different interface or base class for Task-based asynchronous handlers.  This would allow you to write a method with the signature Task ProcessRequestAsync(HttpContext context) rather than having to use the current BeginProcessRequest / EndProcessRequest pattern."

    Thanks.

     

     

     

    Friday, January 28, 2011 1:50 AM
    Moderator
  • Hi Stephen,

    Having read both your ansers here, I'm still not clear how one would go about implementing an async handler using the async CTP. Any guidance would be appreciated.

     

    Here is the scenario I'm trying to work out...in an ASP.NET web forms project.

    1. I've got a business layer class's method that is async. It used to be implemented using the APM but now using async/await 

    2. I have a page that calls this method and all if fine.

    3. I have an async handler that needs to call this method as well. It was all ok when using the APM, but now I'm at a loss for how to:

    a) Implement an asycn Handler using the asycn CTP

    b) Or, if the handler needs to continue to use the APM, then how to call my business layer class's async method from this handler.


    http://www.matlus.com
    Tuesday, November 15, 2011 12:51 PM