33
DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Embed Size (px)

Citation preview

Page 1: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

DEV402

Extending the ASP.NET Runtime

Jurgen PostelmansMicrosoft Regional Director BeLuxU2U

Page 2: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Agenda

Role of the HttpRuntime

What is the HttpRuntimeThe HttpContext class

Extending the HttpRuntimeHttpModules

Global.asax

HttpHandlers

Hosting the HttpRuntime

Page 3: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Using HttpModules Using HttpModules and HttpHandlersand HttpHandlers

demodemo

Page 4: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 5: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 6: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 7: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 8: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

HttpContextEverything you needed to know about the current ASP.NET Request but where afraid to ask

Page 9: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 10: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 11: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Using HttpContextUsing HttpContext

demodemo

Page 12: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Implementing HttpModulesParticipate in every ASP.NET request

Page 13: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

….

Page 14: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 15: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

HttpModules What Are Interception Events?

Request participationMethods called at specific stages of request

HttpRequest HttpResponse available

Terminate processing of request HttpApplication.CompleteRequest()Response.End()

Page 16: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Per-Request Application Events

BeginRequestAuthenticateRequestAuthorizeRequestResolveRequestCacheAquireRequestStatePreRequestHandlerExecuteHandler ExecutionPostRequestHandlerExecuteReleaseRequestStateUpdateRequestCacheEndRequest

Page 17: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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();

}}

Page 18: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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>

Page 19: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 20: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Writing Modules Writing Modules

demodemo

Page 21: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Implementing HttpHandlersTaking control of the request target

Page 22: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 23: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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();

}}

Page 24: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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>

Page 25: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Writing HandlerWriting Handler

demodemo

Page 26: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Writing an Writing an Asynchronous HandlerAsynchronous Handler

demodemo

Page 27: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Hosting the HttpRuntime

Page 28: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 29: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

Hosting ASP.NETHosting ASP.NET

demodemo

Page 30: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 31: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

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

Page 32: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

evaluationsevaluations

Page 33: DEV402 Extending the ASP.NET Runtime Jurgen Postelmans Microsoft Regional Director BeLux U2U

© 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.