Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Asynchronous Pages

0.00/5 (No votes)
18 Oct 2013 1  
Improve the scalability of a website and efficient design for time consuming processes.

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Asynchronous Pages 

Objective: 

Improve the Scalability of website and efficient design for time consuming process.

Note: It should not be confused with Asynchronous request from Client side. AJAX used to improve the end user interface, where as Asynchronous pages handled in server side to provide efficient way of handling the request. It’s particularly used in web pages that include time consuming code that queries database.

Problem: 

.NET maintains a pool of threads to handle the incoming requests, when a request is received ASP.NET assigns a thread from its pool to process the request.

Let’s say we have a code that involves significant waiting time, so if all available threads were assigned to the incoming requests and its executing the time consuming code. The additional requests will be queued; if a limit is reached we will be getting “503 Server Unavailable errors”.

The possible waiting scenarios are: Large data from slow DBRead a file from Remote locationWeb service object…

Resolution: 

If we create Asynchronous pages with time consuming code executing in a separate thread pool which will free up the ASP.NET thread pool to allow it for processing other requests. Once a separate thread completes its work ASP.NET assigns an available thread to complete the operation. This is not going to improve the performance by any way, tricky way to handle all requests effectively making your application scalable.

Example:

Step 1:

<%@ Page Async=”true”%> 

IHttpAsyncHandler will be implemented instead of IHttpHandler.

Step 2: 

AddOnPreRenderCompleteAsync(New BeginEventHandler  
   _(Addressof BeginTask), New EndEventHandler (Addressof EndTask)) 

This will be called when the page loads. 

  • BeginEventHandler - Launches Asynchronous task.
  • EndEventHandler - Handles callback from Asynchronous task.
  • BeginTask and EndTask are the methods assigned to the above delegates.

The page lifecycle will be as follows:

  • Init
  • Load
  • Controlevents
  • Prerender
  • BeginTask()
  • Asynchonous task
  • End Task()
  • PreRenderComplete
  • Savestate
  • Render

Agenda:

  • Initial ASP.NET thread
  • Another ASP.NET thread
  • Thread outside of ASP.NET thread pool

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here