HTTP Request Pipeline Training


Published on

This slide deck is from an Intertech presentation on the HTTP Request Pipeline delivered at VSLive.

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

HTTP Request Pipeline Training

  1. 1. The HTTP Request Pipeline Intertech
  2. 2. Overview  IIS Configuration  Process Architecture  ASP.NET Request Pipeline Overview  HTTP Application  Configuration of HTTP Handlers  Implementing an HTTP Handler  Implementing an HTTP Module
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. IIS – INETINFO.EXE Browse r .ASPX extension mapped to ASPNET_ISAPI.DLL Named Pipes File System ASP.NET Worker Process ASPNET_WP.EXE Your Compiled Code <HTML> . Assembly System.Web. . (Code Behind) UI.Page </HTML> System.Web.UI.Page : IHTTPHandler ASPX Page Rendering
  7. 7. 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
  8. 8. 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…
  9. 9. Context Established HTTP Application Security Events Lifetime Events Lifetime Events ASynch Events HTTPEvents Page Modules Security Events Request Events HTTPContext Request Events Cleanuptypes to HTTP Modules Application Begin Error Headers Custom Init Request Authenticate ASynch Events HTTP Begin Post Handler HTTP Handler Application End Pre-sendto respond Headers Load Resolution Authorize Request State Cache Start Request Release Content Pipeline Implements SessionEvents Pre-send events application Control Context IHTTPHandler Response State Acquisition Update Cache Session End Pre Render Page Handler Web Service Handler Session Pre Handler End Request Remoting Handler Unload variables Server Custom Handlers Cache Cleanup Events Application variables Response Stream Sent To IIS
  10. 10. 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
  11. 11. 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
  12. 12. IHttpHandler Interface  The IHttpHandler interface defines two methods you must implement public bool IsReusable public void ProcessRequest(HttpContext context)  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
  13. 13. 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 (quot;Here is your custom handler output.quot;); }
  14. 14. Configuring the Handler  An entry must be made in the Web.Config to map your custom extension to the handler <httpHandlers> <add path=quot;*.domquot; type=quot;MyHandlerquot; verb=quot;*quot;/> </httpHandlers>  You must also map the extension to the Framework from within IIS
  15. 15. Demo Custom Http Handler
  16. 16. 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
  17. 17. 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
  18. 18. Demo Inheriting From HttpApplication
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. 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() {} }
  23. 23. HttpModule Configuration <configuration> <system.web> … <httpModules> <add name=“ModuleNamequot; type=“Namespace.ClassName,AssemblyNamequot;/> </httpModules> … </system.web> </configuration>
  24. 24. Demo HttpModule Implementation
  25. 25. 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
  26. 26. Help | About Dominic Selly  Instructor for Intertech Training – Now Offering Live Instructor Lead Virtual Training!  – Full course outlines and descriptions Questions? Comments?