asynchronous handlers in asp.net
DESCRIPTION
Here the speaker dTRANSCRIPT
Asynchronous Handlers in ASP.NET
KolkataGeeks 1
HTTP Handlers
• An ASP.NET HTTP handler is a process, which runs in response to a request made to an ASP.NET web application.
Handler Description
.aspx Default handler for all ASP.NET Web Pages
.asmx Default handler for all Web Service Pages
.ashx Default handler for all Web Handlers, that have no UI, instead have @WebHandler directive
KolkataGeeks 2
Hierarchy
IHttpHandler interfacevoid ProcessRequest(HttpContext ctx); bool IsReusable {get;}
IHttpAsyncHandler : IHttpHandler IAsyncResult BeginProcessRequest(HttpContext ctx, AsyncCallback cb, object obj); void EndProcessRequest(IAsyncResult ar);
Implemented By Synchronous Handler Implemented By Asynchronous Handler
KolkataGeeks 3
Synchronous Handler
• A synchronous handler returns only after complete processing of the HTTP request, for which it is called.
KolkataGeeks 4
Synchronous Handler
CLIENT
THREAD POOL
R1
T1
REQUEST QUEUE
ERROR 503
KolkataGeeks 5
KolkataGeeks 6
KolkataGeeks 7
KolkataGeeks 8
Registering Http Handler in Web.Config file
KolkataGeeks 9
KolkataGeeks 10
Asynchronous Handlers
• Asynchronous programming helps in better usage of resources.
• In ASP.Net when a request is made, a thread is created and allocated to the handler file. the thread will be idle when the file will undergo i/o processing.
• So to properly use the thread's idle time , we can release the thread back to thread pool after passing the execution to handler file.
• When the I/O operation is complete, the framework reassigns a thread to the request and the handler can render the output to the browser.
KolkataGeeks 11
Asynchronous HandlerCLIENT
BEGIN
THREAD POOL
END
I/O PROCESS
Thread 1
Thread 2
Thread 1 Released
KolkataGeeks 12
IAsyncResult
• An asynchronous HTTP Handler uses an IAsyncResult object to notify when the operation has been completed.
• Once the I/O or DB operation is completed, thread is reassigned, ASP.NET will continue the execution of the handler.
KolkataGeeks 13
IsReusable Property
• If the handler returns static content, it is safe to be reusable. But if the handler returns dynamic content, to make it thread safe, IsReusable property should be set as false.
• In such case, context switching may occur, which may cause the handler to give wrong output.
KolkataGeeks 14
• The verb=”*” attribute, we instruct the handler to process a request that uses any verb(for eg. POST,HEAD,GET, and so on.)
• In the path=“*.sync” attribute, we instruct the handler to process any incoming requests for files with the .sync extension.
• Type=“handler class name”
KolkataGeeks 15
HTTPContext
• Encapsulates all HTTP-specific information about an individual HTTP request.
KolkataGeeks 16
KolkataGeeks 17
KolkataGeeks 18
KolkataGeeks 19
Register Http Handler in Web.Config File
KolkataGeeks 20
KolkataGeeks 21
If registration of Handler is not done in Web.Config File
KolkataGeeks 22
Now Handler is registered for *.jpg in Web.Config File
KolkataGeeks 23
KolkataGeeks 24