DCSIMG
Handling ASP.NET Async pages client disconnection - Pini Dayan

Pini Dayan

The best thing about a boolean is even if you are wrong, you are only off by a bit.

Handling ASP.NET Async pages client disconnection

Lately I have been working a lot with ASP.NET amazing feature :Asynchronous  pages. Whenever the page has a lot of work in the server and he wishes to set free the thread handling this request, this is the time we need to use Async pages.

I will not go into details about what Async pages are, but in a nutshell: Since there is a limit to the thread ASP.NET can use , we wish to free the long bounded thread to other thread so that they will not wait. (A good example can be: a page calling web service or doing IO work)

Now what is the operation the BeginProcessRequest does is very long, how do we know that the client is still connected and we need to send him back the response?

In addition to Async pages we can even write an HTTPHandler that is Async and will show one to illustate the problem:

   public class CallMeAsyncHandler : IHttpAsyncHandler

   {

         private HttpContext oCurrent;

         public void ProcessRequest(HttpContext context){}

 

         public bool IsReusable

         {

             get

             {

                 return false;

             }

         }

 

   public IAsyncResult BeginProcessRequest

                  (HttpContext context, AsyncCallback cb, object extraData)

   {

     //Do some log operation here

     //Create some IAsync result and return it

     return oAsync;

   }

 

   public void EndProcessRequest(IAsyncResult oAsyncResult)

   {

       //Write back to the output (Response object for instance)

   }

The answer is very simple,as it turns out the Response object has a property specifying the client is still connected : Response.IsClientConnected.

Hope this helps, enjoy.

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: