asynchronous handlers in asp.net

24
Asynchronous Handlers in ASP.NET KolkataGeeks 1

Upload: abhishek-sur

Post on 10-May-2015

2.174 views

Category:

Technology


2 download

DESCRIPTION

Here the speaker d

TRANSCRIPT

Page 1: Asynchronous handlers in asp.net

Asynchronous Handlers in ASP.NET

KolkataGeeks 1

Page 2: Asynchronous handlers in asp.net

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

Page 3: Asynchronous handlers in asp.net

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

Page 4: Asynchronous handlers in asp.net

Synchronous Handler

• A synchronous handler returns only after complete processing of the HTTP request, for which it is called.

KolkataGeeks 4

Page 5: Asynchronous handlers in asp.net

Synchronous Handler

CLIENT

THREAD POOL

R1

T1

REQUEST QUEUE

ERROR 503

KolkataGeeks 5

Page 6: Asynchronous handlers in asp.net

KolkataGeeks 6

Page 7: Asynchronous handlers in asp.net

KolkataGeeks 7

Page 8: Asynchronous handlers in asp.net

KolkataGeeks 8

Page 9: Asynchronous handlers in asp.net

Registering Http Handler in Web.Config file

KolkataGeeks 9

Page 10: Asynchronous handlers in asp.net

KolkataGeeks 10

Page 11: Asynchronous handlers in asp.net

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

Page 12: Asynchronous handlers in asp.net

Asynchronous HandlerCLIENT

BEGIN

THREAD POOL

END

I/O PROCESS

Thread 1

Thread 2

Thread 1 Released

KolkataGeeks 12

Page 13: Asynchronous handlers in asp.net

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

Page 14: Asynchronous handlers in asp.net

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

Page 15: Asynchronous handlers in asp.net

• 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

Page 16: Asynchronous handlers in asp.net

HTTPContext

• Encapsulates all HTTP-specific information about an individual HTTP request.

KolkataGeeks 16

Page 17: Asynchronous handlers in asp.net

KolkataGeeks 17

Page 18: Asynchronous handlers in asp.net

KolkataGeeks 18

Page 19: Asynchronous handlers in asp.net

KolkataGeeks 19

Page 20: Asynchronous handlers in asp.net

Register Http Handler in Web.Config File

KolkataGeeks 20

Page 21: Asynchronous handlers in asp.net

KolkataGeeks 21

Page 22: Asynchronous handlers in asp.net

If registration of Handler is not done in Web.Config File

KolkataGeeks 22

Page 23: Asynchronous handlers in asp.net

Now Handler is registered for *.jpg in Web.Config File

KolkataGeeks 23

Page 24: Asynchronous handlers in asp.net

KolkataGeeks 24