You are on page 1of 21

ASP.

NET Pipeline (HttpModule


and HttpHandler)

Sreedhar Koganti
Microsoft MVP (ASP.NET)
http://weblogs.asp.net/skoganti
Contact: http://weblogs.asp.net/skoganti/contact.aspx

--- http://www.w3coder.com
Agenda
Request Process Pipeline architecture intro
Good to know HttpRuntime Architecture
Meet Global.asax, HttpModule
Take advantage of HttpModule
Understand HttpHandler
Meet Lengthy process Life saver Asynchronous
HttpHandler
Q&A
Request Process Pipeline
architecture intro
Worker
Workerprocess
processidentity
identity (w3wp.exe)
(w3wp.exe)
CLR/HttpRuntime
CLR/HttpRuntime

aspnet_isapi
aspnet_isapi
AppDomain
User Mode
Kernel Mode

HTTP.SYS
HTTP.SYS

CodeCamp/Pipelinetest.aspx
IIS and ASP.NET
.NET extensions are configured to be handled by
aspnet_isapi.dll
Peal it and see inside the
pipeline
Good to know HttpRuntime Architecture

.asmx .aspx .ashx

HttpHandlers
HttpRuntime

SessionState
HttpContext

Authentication

OutputCache
HttpModules

Process Boundary

CodeCamp/Pipelinetest.aspx
Good to know HttpRuntime Architecture

HttpRuntime is Logical replacement for ISAPI API


HttpRuntime forwards requests to HttpApplication
Here starts the story…HttpRuntime interns calls:
HttpContext
HttpHandler
HttpModule
Meet Global.asax, HttpModule
Events are fired at several stages in the processing of
requests and responses pass through the pipeline
You can handle these events either in HttpApplication
(global.asax) or in IHttpModule interface
Functionality of global.asax and modules overlap
ASP.NET had it’s own modules
Session State HttpModule
Forms Authentication HttpModule
Output Cache HttpModule
Global.asax supports extra events like
Application_Start and Application_End
Session_Start and Session_End
Global.asax/HttpModule

Demo
Per-Request Application Events
BeginRequest
AuthenticateRequest
AuthorizeRequest
ResolveRequestCache
AquireRequestState
PreRequestHandlerExecute
Target Handler Execution
PostRequestHandlerExecute
ReleaseRequestState
UpdateRequestCache
EndRequest
Take advantage of HttpModule
Class must implement interface:
System.Web.IHttpModule
public interface IHttpModule {
public void Init(HttpApplication application);
public void Dispose();
}
Init()
Called when module is created
Register delegates for runtime events
Dispose()
Registering an HttpModule
Compile into .NET assembly
Deploy
Into application’s \bin directory or
Register in the GAC
Register in configuration:
<configuration>
<system.web>
<httpModules>
<add name=“friendly name”
type=“class, assembly“ />
</httpModules>
</system.web>
</configuration>
Real-time HttpModule
examples

Demo
Understand HttpHandler
Are the endpoint for a request
They are responsible for creating the Http response that is
send back to client
Handlers are similar to ISAPI Extensions

Examples:
ASP.NET Page Handler (*.aspx)
ASP.NET Service Handler (*.asmx)
Server-Side XSL Transformer (*.xml)
Creating an HttpHandler
Class must implement interface:
System.Web.IHttpHandler
public interface IHttpHandler {
public void ProcessRequest(HttpContext context);
public bool IsReusable();
}

ProcessRequest()
‘Main’ method of the handler

IsReusable { get; }
Indicates whether pooling is supported
Registering an HttpHandler
Compile into .NET assembly
Deploy
Into application’s \bin directory or
Register in the GAC
Register in configuration:

<configuration>
<system.web>
<httpHandlers>
<add verb="*" path=“Assembly.FileExtension"

type="class, assembly" />


</httpHandlers>
Important: Extension must be mapped in IIS
</system.web>
</configuration>
Real-time HttpHandler
examples

Demo
Meet Lengthy process Life saver
Asynchronous HttpHandler

Same as normal handler in implimentation.


It implements using IHttpAsyncHandler
IhttpAsyncHandler is derived from IhttpHandler
IHttpAsyncHandler Implimentation
public class ImageAsyncHandler : IHttpAsyncHandler
{
public ImageAsyncHandler()
{}
public IAsyncResult BeginProcessRequest(HttpContext context,
AsyncCallback cb, object extraData)
{}

public void EndProcessRequest(IAsyncResult result)


{
HttpContext context = (HttpContext)result.AsyncState;

}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{}
}
Real-time IHttpAsyncHandler
examples

Demo
It’s Your time….
Want to learn more? here are some references..
Have Questions: Visit http://forums.asp.net/

See below links for more information on


HttpModule/HttpHandler examples:
http://support.microsoft.com/kb/887289
http://aspalliance.com/articleViewer.aspx?aId=442
http://forums.asp.net/thread/1353901.aspx
http://www.codeproject.com/useritems/http-module-ip-
security.asp
http://briandela.com/blog/archive/2005/06/29/652.aspx

You might also like