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