Extending ASP.
NET
Brian Noyes Principal Software Architect IDesign, Inc. ([Link])
About Brian
Principal Software Architect, IDesign Inc. ([Link]) Microsoft MVP in [Link] Writing
MSDN Magazine, [Link], Visual Studio Magazine, .NET Developers Journal Building Windows Forms Data Applications with .NET 2.0, Addison-Wesley, expected release spring 2005
Speaking
Microsoft TechEd, Visual Studio Connections, DevEssentials, VSLive!, INETA Speakers Bureau
Participate in Microsoft design reviews E-mail: [Link]@[Link] Blog: [Link]
Copyright 2005 IDesign, Inc. IDesign,
Agenda
[Link] Extensibility Overview Request Processing Pipeline Configuring [Link] Extensibility Handlers Applications Modules Advanced Handler Topics
[Link] Extensibility Overview
Extension Points
Application
[Link], code-behind
Module
Custom classes
Handler
Pages, custom classes, asynchronous handlers, handler factories
[Link] 2.0
Custom providers
Copyright 2005 IDesign, Inc. IDesign,
Agenda
[Link] Extensibility Overview Request Processing Pipeline Configuring [Link] Extensibility Handlers Applications Modules Advanced Handler Topics
Request Processing Pipeline
Overview
IIS and ISAPI De-emphasized [Link] Manages:
Worker processes AppDomains Threads Recycling Application, Module, Handler instances
Copyright 2005 IDesign, Inc. IDesign,
Request Processing Pipeline
Servicing Requests
Ultimate goal of a web application
Process request Return response
Internet Information Server (IIS) front end
Usually Could be Cassini, Apache, etc.
Request Processing Pipeline
IIS Processing
The The Net Net Request Response
IIS
aspnet_isapi.dll
[Link]
Other ISAPI Ext.
Copyright 2005 IDesign, Inc. IDesign,
Request Processing Pipeline
[Link] Forwarding
IIS
aspnet_isapi.dll
aspnet_wp.exe
AppDomain1 (Virtual Directory 1) AppDomain2 (Virtual Directory 2)
... (multiple worker processes)
Request Processing Pipeline
Runtime processing
aspnet_wp.exe
Request
HttpRuntime
HttpContext
Response
App Factory
HttpApplication
HttpModule
Handler Factory
HttpHandler
Copyright 2005 IDesign, Inc. IDesign,
Request Processing Pipeline
HttpContext
Wraps HTTP request context
All associated state
Members: Request Session Items Error User
Response Application Server AllErrors
Agenda
[Link] Extensibility Overview Request Processing Pipeline Configuring [Link] Extensibility Handlers Applications Modules Advanced Handler Topics
Copyright 2005 IDesign, Inc. IDesign,
Configuring [Link] Extensibility
Configuration Files
Application sub-folder Virtual Directory Site Machine
[Link] [Link] [Link] [Link]
Configuring [Link] Extensibility
[Link] elements
ProcessModel
Configure worker process [Link] only
httpHandlers
Attach handers to request and file type All config levels
httpModules
Attach modules for filtering Machine, site, virtual directory levels
Copyright 2005 IDesign, Inc. IDesign,
Agenda
[Link] Extensibility Overview Request Processing Pipeline Configuring [Link] Extensibility Handlers Applications Modules Advanced Handler Topics
Handlers
Overview
Request target or endpoint Process request, return response Attached through config files Implements IHttpHandler interface
Copyright 2005 IDesign, Inc. IDesign,
Handlers
Built-in handlers
PageHandlerFactory (*. aspx) SimpleHandlerFactory (*.ashx) WebServiceHandlerFactory (*.asmx) HttpRemotingHandlerFactory (*.rem, *. soap) HttpForbiddenHandler (*.cs, *config, etc.) StaticFileHandler (* GET,HEAD) HttpMethodNotAllowedHandler (*)
Handlers
Custom Handler Examples
Dynamic image generation Dynamic content generation
HTML, XML/XSLT, etc.
Distributed computational process invocation Sound file alerts
Copyright 2005 IDesign, Inc. IDesign,
Handlers
Custom Handler Requirements
Implement IHttpHandler Compile assembly and place in bin folder Configure file extension in [Link] Configure file extension in IIS
Custom Handlers
Implement IHttpHandler
public class Simple Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { } public bool IsReusable { get { } } }
Copyright 2005 IDesign, Inc. IDesign,
Custom Handlers
Configuring Handlers
<httpHandlers> section
<add>
verb (GET,POST,HEAD,PUT,*) path (file spec) type ([Link],assembly)
<remove>
verb path
<clear>
Custom Handlers
Configuring Handlers
<[Link]> <httpHandlers> <add verb="GET" path="*.myext" type=[Link], CustomHandlerLib1" /> </httpHandlers> </[Link]>
Copyright 2005 IDesign, Inc. IDesign,
Custom Handlers
Configuring IIS
Virtual Directory Properties
Directory Tab
Application Settings Configuration Button
Application Mappings
Demo
Custom Handlers
Brian Noyes IDesign, Inc. [Link]
Copyright 2005 IDesign, Inc. IDesign,
Custom Handlers
SimpleHandlers
Created by the SimpleHandlerFactory .ashx file containing:
Handler class definition WebHandler directive
Compiled and run first time it is called
Custom Handlers
SimpleHandlers
Advantages:
No IIS config required No entries in .NET config files required xcopy deployment
Disadvantages
One time compilation penalty Errors not detected until runtime No IntelliSense or color coding
Copyright 2005 IDesign, Inc. IDesign,
Demo
Simple Handlers
Brian Noyes IDesign, Inc. [Link]
Custom Handlers
Best practices
Indicate reusability correctly Store state where it belongs Process requests quickly Prefer custom handlers for invisible processing Prefer compiled handlers over simple handlers
Copyright 2005 IDesign, Inc. IDesign,
Agenda
[Link] Extensibility Overview Request Processing Pipeline Configuring [Link] Extensibility Handlers Applications Modules Advanced Handler Topics
Custom Applications
Overview
Declared through [Link] file Class derived from HttpApplication Code behind or script block Wire up event handlers
Explicit or implicit
Any additional methods, properties, fields, events Multiple instances used to process requests
Copyright 2005 IDesign, Inc. IDesign,
Custom Applications
Lifecycle Events
BeginRequest AuthenticateRequest AuthorizeRequest ResolveRequestCache AcquireRequestState PreRequestHandlerExecute PostRequestHandlerExecute ReleaseRequestState UpdateRequestCache EndRequest
User Property valid Handler Created
ProcessRequest() Called
Custom Applications
Unordered Events
Disposed Error PreSendRequestContent PreSendRequestHeaders
Copyright 2005 IDesign, Inc. IDesign,
Custom Applications
Application Scope Events
Application_Start Application_End Session_Start Session_End
Demo
Simple Custom Application
Brian Noyes IDesign, Inc. [Link]
Copyright 2005 IDesign, Inc. IDesign,
Agenda
[Link] Extensibility Overview Request Processing Pipeline Configuring [Link] Extensibility Handlers Applications Modules Advanced Handler Topics
HTTP Modules
Overview
Request processing filters Plug into pipeline between application and handler Added through config file entries Pre-process requests coming in Post-process requests going out Handle application events
Copyright 2005 IDesign, Inc. IDesign,
HTTP Modules
Built-in Modules
OutputCacheModule SessionStateModule WindowsAuthenticationModule FormsAuthenticationModule PassportAuthenticationModule UrlAuthorizationModule FileAuthorizationModule
HTTP Modules
Module Examples
Custom security Response Stream filtering User tracking and logging Custom caching/state maintenance Front end redirect and URL parsing
Copyright 2005 IDesign, Inc. IDesign,
Custom Modules
Implementation Requirements
Class that implements IHttpModule Handle events of interest Compile and place in bin folder Configure in [Link] or [Link]
Custom Modules
Implement IHttpModule
public class Simple Handler : IHttpModule { public void Init(HttpApplication app) { // Subscribe to events of interest } public void Dispose() { // clean up } }
Copyright 2005 IDesign, Inc. IDesign,
Custom Handlers
Configuring Handlers
<httpModules> section
<add>
name type ([Link],assembly)
<remove>
name
<clear>
Custom Handlers
Configuring Handlers
<httpModules> <remove name="FormsAuthentication"/> <add name="MyFormsAuthentication" type="[Link], Module1"/> </httpModules>
Copyright 2005 IDesign, Inc. IDesign,
Demo
Custom Modules
Brian Noyes IDesign, Inc. [Link]
Custom Modules
Gotchas
Know the request lifecycle Be careful with redirects Stubbing Event Handling Module processing order Dont be greedy
Copyright 2005 IDesign, Inc. IDesign,
Agenda
[Link] Extensibility Overview Request Processing Pipeline Configuring [Link] Extensibility Handlers Applications Modules Advanced Handler Topics
Advanced Handler Topics
Asynchronous Handlers
Perform processing on a self-managed thread
Dont tie up threads from the thread pool
Class that implements IHttpAsyncHandler Uses standard .NET Async pattern
BeginProcessRequest EndProcessRequest Register a callback method
Copyright 2005 IDesign, Inc. IDesign,
Asynchronous Handlers
Normal request handling performed on thread from process-wide thread pool
25 threads by default configurable through <processModel> in [Link]
Asynchronous handlers allow you to perform lengthy processing on a separate (self-managed) thread
Asynchronous Handlers
Requirements:
Same as normal handlers, except implement IHttpAsyncHandler (derives from IHttpHandler)
Basics:
ProcessRequest() will never be called Spin your own worker thread in BeginProcessRequest() and use passed in context object
Optionally can pass a state object Optionally create an object derived from IAsyncResult and return
Notify [Link] when processing is complete through AsyncCallback Clean up resources when EndProcessRequest() is called if necessary
Copyright 2005 IDesign, Inc. IDesign,
Async Handler Processing
Implement IHttpAsyncHandler
Copyright 2005 IDesign, Inc. IDesign,
Handler Factories
Handler factories create handler instances for the application Can be used to perform custom handler pooling or for custom creation semantics Requirements
Same as for custom handlers, except:
Define class that implements IHttpHandlerFactory Configure that as the handler in [Link] or [Link]
Implement IHttpHandlerFactory
Copyright 2005 IDesign, Inc. IDesign,
Session Summary
Multiple extensibility points in [Link] Handlers for custom processing of requests Applications for custom lifecycle event handling
Non-portable across apps
Modules for custom lifecycle event handling and request filtering
Portable across apps
Async Handlers and Handlers Factories for advanced scenarios
Resources
Extend [Link] With HTTP Modules, [Link] Feb 2003 Handle Requests Asynchronously, Brian Noyes, DotNetDashboard, [Link] d=68 Securely Implement Request Processing, Filtering, and Content Redirection with HTTP Pipelines in [Link], Tim Ewald and Kieth Brown, MSDN Magazine Sep 2002 Essential [Link] with Examples in C# or Essential [Link] with Examples in Visual Basic .NET, Fritz Onion, Addison-Wesley (ISBN 0-201-76040-1 / 0-201-76039-8)
E-mail: [Link]@[Link] Blog: [Link] Blog: [Link]
Copyright 2005 IDesign, Inc. IDesign,