HTTP Request Pipeline Training
-
Upload
intertech-training -
Category
Technology
-
view
3.759 -
download
1
description
Transcript of HTTP Request Pipeline Training
The HTTP Request Pipeline
Intertech
www.Intertech.com
Overview
IIS Configuration
Process Architecture
ASP.NET Request Pipeline Overview
HTTP Application
Configuration of HTTP Handlers
Implementing an HTTP Handler
Implementing an HTTP Module
IIS Configuration
The ASP.NET Framework uses a COM ISAPI application
This ISAPI extension communicates with a Managed process via named pipes
The ASP.NET Framework runs within its own process – ASPNET_WP.EXE
Within this architecture, IIS delegates requests to the worker process
Delegation of Requests
Requests are mapped to specific ISAPI applications within IIS application configuration
Each extension can be mapped to a different application
Classic ASP was configured this way as well
From COM to Managed Code
Once IIS has delegated the request to ASPNET_ISAPI.DLL, it gets passed to managed code via named pipes
Requests for any file type can be mapped to the ASP.NET Framework
38 file types mapped to this ISAPI application when the Framework is installed
Extend functionality of Framework with your own custom types
Map old extensions to Framework to migrate users of old site to your .NET application
File System
IIS – INETINFO.EXE
.ASPX extension mapped to
ASPNET_ISAPI.DLL
Browse
r
ASP.NET Worker Process
ASPNET_WP.EXE
ASPX
Assembly(Code Behind)
Your Compiled Code
System.Web.UI.Page : IHTTPHandler
Nam
ed
Pip
es
System.Web.
UI.Page
Page Rendering
<HTML>
.
.
</HTML>
HTTP Handlers
The previous picture describes the Page Handler specifically.
Once a request is mapped to the ASP.NET Framework from IIS, it can be directed to a specific handler within the framework
There are many handlers that install by default with the ASP.NET Framework– Web Services
– Remoting
– Trace Handler
– Image Service
– Many others
The HTTP Application Lifetime
Instances of handlers are actually created by the HTTPApplication type
The HTTPApplication uses a consistent lifecycle across all handlers
This lifecycle is therefore extensible across different types of handlers
The Page object’s lifecycle is only a small part of this process
The Page object exposes its own consistent lifecycle you’re familiar with from ASP.NET page processing
– This lifecycle is defined by the System.Web.UI.Control base class that the Page and all controls inherit from
Let’s examine the entire pipeline…
Reque
st
Pip
elin
eHTTP ApplicationSecurity Events
Lifetime Events
Request Events
HTTP Modules
ASynch Events
Context
Established
Conte
xt
HTTP HandlerImplements
IHTTPHandler
Page Handler
Web Service Handler
Remoting Handler
Custom Handlers
Cleanup Events
Response
Stream Sent
To IIS
HTTPContext
HTTP Headers
Request
Response
Session
Server variables
Cache
Application variables
Security Events
Authenticate
Authorize
Lifetime Events
Application Begin
Application End
Session Start
Session End
Request Events
Begin Request
Cache Resolution
State Acquisition
Pre Handler
HTTP Modules
Custom types to
respond to
application events
ASynch Events
Error
Pre-send Headers
Pre-send Content
Page Events
Init
Load
Control Events
Pre Render
Unload
Cleanup Events
Post Handler
Release State
Update Cache
End Request
Handlers in the Framework
Handlers are configured in the Machine.Config
– This is where specific extensions are mapped to different implementations of the IHTTPHandler Interface
Page Handler : ASPX pages
– .aspx
Web Service Handler – Web Services
– .asmx
HTTP Remoting Handler – Remoting
– .rem and .soap
HTTP Forbidden Handler – returns 404s for protected file types
– Projects, code files, config files, webinfo, resource files, binaries, user controls
Handler Uses
Secure file types that are not secured under .NET Forms based authentication– Map .doc and .pdf to framework
Provide link updates from old technology– Map CFM or ASP to Framework
– Redirect to new ASPX page
Add copyright to images before returning them– Load requested image as Bitmap object
– Use GDI+ to paint a ‘watermark’ on your images
Retrieve documents from database – URLs appear to be file locations to user
IHttpHandler Interface
The IHttpHandler interface defines two methods you must implement
IsReusable – boolean indicating if instances of the type can be reused across requests
ProcessRequest – the hand off from the HttpApplication to your own handling of the request
public bool IsReusable
public void ProcessRequest(HttpContext context)
Implementing IHttpHandler
Notice the ProcessRequest method accepts an instance of HttpContext as a parameter– Use this to send content down the output stream
public bool IsReusable{
get { return true; }}
public void ProcessRequest(HttpContext context){
context.Response.Write("Here is your custom handler output.");
}
Configuring the Handler
An entry must be made in the Web.Config to map your custom extension to the handler
<httpHandlers><add path="*.dom" type="MyHandler" verb="*"/>
</httpHandlers>
You must also map the extension to the Framework from within IIS
Demo
Custom Http Handler
Extending the Request Pipeline
The easiest way to extend the ASP.NET request processing pipeline is to create a class that inherits from HttpApplication and add it to your project– This is done for you by default by Visual Studio.NET in
the global.asax file
You can trap all standard events in the lifetime of the request and add your custom logic
Reuse of HttpApplication Types
The global.asax is deployed on a per IIS application basis
This limits your implementation to a single web site
To reuse functionality across different applications, create type that inherits from HttpApplication in stand-alone assembly
Add reference from a web project
Have global.asax inherit from your custom type instead of directly from HttpApplication
Assembly can then be reused across applications
Demo
Inheriting From HttpApplication
Creating Custom HttpModules
HttpApplication hooks implemented with HttpModules under the hood
Session management – In or out of process
Authentication– For Forms Authentication
Output Caching – Check cache before request comes in
– Return from cache instead of creating instance of page object when found
Custom HttpModules
You can extend the pre and post handler pipeline with your own HttpModules
Create implementation of IHttpModule interface
Register into pipeline using Web.Config
Interface exposes only two methods– Init & Dispose
Use Init to sink events in the HttpApplication lifecycle
Implementing IHttpModule
Init method accepts instance of HttpApplication as argument
All of ASP.NET intrinsics are exposed
Use application instance to sink any events in lifetime of request
Squirrel away local reference to instance
In event traps, examine the path, the user, the Http headers, etc
Sample Implementation
public class UserDir : HttpApplication{private HttpApplication app;public void Init(HttpApplication application){app = application;app.BeginRequest += new EventHandler(this.CustomEventTrap);}
private void CustomEventTrap(object sender, EventArgs e){ //Custom Logic using ‘app’ Implemented Here }
public void Dispose() {}}
HttpModule Configuration
<configuration><system.web>…<httpModules><add name=“ModuleName"
type=“Namespace.ClassName,AssemblyName"/></httpModules>…</system.web></configuration>
Demo
HttpModule Implementation
Summary
ASP.NET Framework is complete application server platform– Not just for Web Forms
.NET Framework ships with several different applications built into ASP.NET Framework– Web Forms
– Web Services
– Remoting
ASP.NET Framework is completely extensible– Build your own handlers
– Inherit from HttpApplication
– Extend the pipeline with Http Modules
Help | About Dominic Selly
Instructor for Intertech Training – Now Offering Live Instructor Lead Virtual Training!
www.IntertechTraining.com– Full course outlines and descriptions
Questions? Comments?