Upload
vivien-lee
View
220
Download
1
Embed Size (px)
Citation preview
DEV402
Extending the ASP.NET Runtime
Jurgen PostelmansMicrosoft Regional Director BeLuxU2U
Agenda
Role of the HttpRuntime
What is the HttpRuntimeThe HttpContext class
Extending the HttpRuntimeHttpModules
Global.asax
HttpHandlers
Hosting the HttpRuntime
Using HttpModules Using HttpModules and HttpHandlersand HttpHandlers
demodemo
Role of the Http Runtime
Pluggable, modular architectureCaching, Security, State Management….
Ability to customize and extend ASP.NETExample: Perform dynamic authorizationExample: Take over .xml file extension
Reduce need for C/C++ ISAPIsGoal: You can do everything in ASP.NETUse any managed language, including VB .NET
HTTPHTTPRequest Request HandlerHandler
ASP.NET ArchitectureInside IIS 5.0
INETINFO.EXEINETINFO.EXE(IIS 5.0)(IIS 5.0)
aspnet_isapi.dllaspnet_isapi.dll
ASPNET_WP.EXEASPNET_WP.EXEWorker ProcessWorker Process
Named pipes
http://www.foo.com/bar.aspxhttp://www.foo.com/bar.aspx
ASP.NET ArchitectureInside IIS 5.0
HTTP.SYSHTTP.SYSHTTP.SYSHTTP.SYS
User Mode
Kernel Mode
ASP.NETASP.NET
FiltersFilters
Worker Worker ProcessProcess
ASP.NETASP.NET
FiltersFilters
Worker Worker ProcessProcess
ASP.NETASP.NET
FiltersFilters
Worker Worker ProcessProcess
WASWASWASWAS
INETINFOINETINFO
metabasemetabase
AppPool 1AppPool 1 AppPool 2AppPool 2
Http Runtime Architecture
HttpHandlers
HttpModulesHtt
pR
un
tim
eH
ttp
Ru
nti
me
.asmx.asmx .aspx.aspx .ashx.ashx
OutputCacheOutputCache
AuthenticationAuthentication
SessionState SessionState
Process BoundaryProcess Boundary
Cache
External Session
Htt
pC
on
text
HttpContextEverything you needed to know about the current ASP.NET Request but where afraid to ask
About HttpContext
The glue that ties the pipeline togetherEncapsulates all request specific data
Instance created for each requestSystem.Web.HttpContext
“flows” throughout request lifetime
Developers can add to HttpContextHttpContext.Items collection
Objects then flowed throughout request
Exposed via static HttpContext.CurrentCode must be running on original thread
HttpContext Properties
Name Type DescriptionCurrent HttpContext Context for the current request
Application HttpApplicationState Appliction-wide property bag
ApplicationInstance HttpApplication Application Context (Modules)
Session HttpSessionState Per-client session state
Request HttpRequest The HTTP Request
Response HttpResponse The HTTP Response
User IPrincipal Principal information
Handler IHttpHandler HTTP Handler that will dispatch the call
Items IDictionary Per-request property bag
Server HttpServerUtility URL cracking/sub handlers
Error Exception First error occured during processing
Using HttpContextUsing HttpContext
demodemo
Implementing HttpModulesParticipate in every ASP.NET request
HttpModules
Enables developers to: Extend the HTTP Pipeline
Examine and modify HTTP Requests
Used by ASP.NET:External Session State HttpModule
Forms Authentication HttpModule
Output Cache HttpModule
….
HttpRuntime Architecture
HttpModulesHtt
pR
un
tim
eH
ttp
Ru
nti
me
HttpHandlers
.aspx.aspx .asmx.asmx .ashx.ashx
OutputCacheOutputCacheOutputCacheOutputCache
AuthenticationAuthenticationAuthenticationAuthentication
SessionState SessionState SessionState SessionState
Htt
pC
on
text
OutputOutput CachedCached PagePage Cache
External Session
HttpModules What Are Interception Events?
Request participationMethods called at specific stages of request
HttpRequest HttpResponse available
Terminate processing of request HttpApplication.CompleteRequest()Response.End()
Per-Request Application Events
BeginRequestAuthenticateRequestAuthorizeRequestResolveRequestCacheAquireRequestStatePreRequestHandlerExecuteHandler ExecutionPostRequestHandlerExecuteReleaseRequestStateUpdateRequestCacheEndRequest
Creating an HttpModule
Class must implement interface:System.Web.IHttpModule
Init()Called when module is created
Register delegates for runtime events
Dispose()
public interface IHttpModule {public interface IHttpModule {
public void Init(HttpApplication application);public void Init(HttpApplication application);public void Dispose();public void Dispose();
}}
Registering an HttpModule
1. Compile into .NET assembly2. Deploy
Into application’s \bin directory orRegister in the GAC
3. Register in configuration:
<configuration> <configuration> <system.web> <system.web> <httpModules> <httpModules> <add name=“friendly name”<add name=“friendly name” type=“class, assembly“ />type=“class, assembly“ /> </httpModules> </httpModules> </system.web></system.web></configuration></configuration>
Modules vs Global.asax
Functionality of global.asax and modules overlap
Global.asax supports extra events likeApplication_Start and Application_End
Session_Start and Session_End
Writing Modules Writing Modules
demodemo
Implementing HttpHandlersTaking control of the request target
What are HttpHandlers ?
Are the endpoint for a requestThey actually process the request and generate the response (HTML, XML,…)
Similar to ISAPI Extensions
Examples:ASP.NET Page Handler (*.aspx)
ASP.NET Service Handler (*.asmx)
Server-Side XSL Transformer (*.xml)
Dynamic image generation
Creating an HttpHandler
Class must implement interface:System.Web.IHttpHandler
ProcessRequest()‘Main’ method of the handler
IsReusable { get; }Indicates whether pooling is supported
public interface IHttpHandler {public interface IHttpHandler {public void ProcessRequest(HttpContext context);public void ProcessRequest(HttpContext context);public bool IsReusable();public bool IsReusable();
}}
Registering an HttpHandler
1. Compile into .NET assembly2. Deploy
Into application’s \bin directory orRegister in the GAC
3. Register in configuration:
4. Important: Extension must be mapped in IIS
<configuration> <configuration> <system.web> <system.web> <httpHandlers> <httpHandlers>
<add verb="*" path="myfile.myext" <add verb="*" path="myfile.myext" type="class, assembly" /> type="class, assembly" /> </httpHandlers> </httpHandlers> </system.web> </system.web></configuration></configuration>
Writing HandlerWriting Handler
demodemo
Writing an Writing an Asynchronous HandlerAsynchronous Handler
demodemo
Hosting the HttpRuntime
ASP.NET Hosting
HttpRuntime is a clean abstractionEnable non-IIS hosting scenarios
System.Web.Hosting namespace
Scenarios:Pre-generation of .htm for dynamic pages
ASP.NET Cassini server (XP Home)See http://www.asp.net/Projects/Cassini/Download/Default.aspx?tabindex=0&tabid=1 for sample source
Hosting ASP.NETHosting ASP.NET
demodemo
Community Resources
Community Resourceshttp://www.microsoft.com/communities/default.mspx
Most Valuable Professional (MVP)http://www.mvp.support.microsoft.com/
NewsgroupsConverse online with Microsoft Newsgroups, including Worldwidehttp://www.microsoft.com/communities/newsgroups/default.mspx
User GroupsMeet and learn with your peershttp://www.microsoft.com/communities/usergroups/default.mspx
Suggested Reading And Resources
The tools you need to put technology to work!The tools you need to put technology to work!
TITLETITLE AvailableAvailable
Microsoft® ASP.NET Microsoft® ASP.NET Programming with Microsoft Programming with Microsoft Visual C#® .NET Version 2003 Visual C#® .NET Version 2003 Step By Step: 0-7356-1935-6Step By Step: 0-7356-1935-6
TodayToday
Microsoft Press books are 20% off at the TechEd Bookstore
Also buy any TWO Microsoft Press books and get a FREE T-Shirt
evaluationsevaluations
© 2003 Microsoft Corporation. All rights reserved.© 2003 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.