DEV 410 Inside the ASP NET Runtime Intercepting

  • Slides: 58
Download presentation
DEV 410 Inside the ASP. NET Runtime - Intercepting HTTP Requests Michele Leroux Bustamante

DEV 410 Inside the ASP. NET Runtime - Intercepting HTTP Requests Michele Leroux Bustamante Principal Software Architect IDesign Inc.

Speaker BIO Principal Software Architect, IDesign, www. idesign. net Microsoft Regional Director, www. microsoft.

Speaker BIO Principal Software Architect, IDesign, www. idesign. net Microsoft Regional Director, www. microsoft. com/rd Microsoft MVP – XML Web Services INETA Speaker’s Bureau BEA Technical Director Web Services Program Advisor, UCSD Blog: www. dasblonde. net

What we will cover: How IIS passes requests to ASP. NET How to configure

What we will cover: How IIS passes requests to ASP. NET How to configure the ASP. NET pipeline to intercept requests, and why you’d want to How to create useful custom pipeline components HTTP Modules Handler Factories and Handlers SOAP Extensions

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers SOAP Extensions

IIS & ASP. NET Runtime Configuration IIS passes HTTP requests to ASP. NET through

IIS & ASP. NET Runtime Configuration IIS passes HTTP requests to ASP. NET through an unmanaged ISAPI extension This is the “hook” between IIS and the ASP. NET HTTP runtime Resource extensions are configured in IIS to be handled by ASP. NET Default configuration excludes *. xml, *. html, graphic file extensions and more

IIS & ASP. NET Runtime Configuration. NET extensions are configured to be handled by

IIS & ASP. NET Runtime Configuration. NET extensions are configured to be handled by aspnet_isapi. dll

IIS & ASP. NET Request Workflow IIS ASP. NET Runtime Application *. asmx HTTP

IIS & ASP. NET Request Workflow IIS ASP. NET Runtime Application *. asmx HTTP Request HTTP Response *. aspnet_isapi. dll Web. config asp. dll Process Request Machine. config

ASP. NET Configuration Machine. config defines default handlers or handler factories to manage requests

ASP. NET Configuration Machine. config defines default handlers or handler factories to manage requests <http. Handlers> <add verb="*" path="*. aspx" type="System. Web. UI. Page. Handler. Factory"/> <add verb="*" path="*. asmx" type="System. Web. Services. Protocols. Web. Service. Handler. Factory, System. Web. Services, …/> <add verb="*" path="*. soap" type="System. Runtime. Remoting. Channels. Http. Remoting. Handler. Fac tory, System. Runtime. Remoting, …/> <add verb="*" path="*. config" type="System. Web. Http. Forbidden. Handler"/> </http. Handlers>

ASP. NET Configuration Web. config may alter Machine. config settings at the application level

ASP. NET Configuration Web. config may alter Machine. config settings at the application level HTTP Handlers can be chosen by: Verb: POST/GET Path: by extension, by specific URL <http. Handlers> <add verb=“GET" path="*. xml" type=“Dot. Net. Dashboard. Web. File. Download. Handler, Dot. Net. Dashboard. Web, Version=1. 0. 0. 0, Culture=neutral, Public. Key. Token=xxx"/> <add verb="*" path=“display. Image. aspx" type=“Dot. Net. Dashboard. Web. Image. Formatter, Dot. Net. Dashboard. Web, Version=1. 0. 0. 0, Culture=neutral, Public. Key. Token=xxx” /> <add verb="*" path=“*. xls” type="System. Web. Http. Forbidden. Handler"/> </http. Handlers>

IIS & ASP. NET Request Workflow IIS ASP. NET Runtime Application *. asmx HTTP

IIS & ASP. NET Request Workflow IIS ASP. NET Runtime Application *. asmx HTTP Request *. aspnet_isapi. dll Web. config asp. dll IHttp. Handler. Factory IHttp. Handler HTTP Response Process Request Machine. config

IIS 5. 0 and ASP. NET IIS 5. 0 ASP. NET aspnet_wp. exe Thread

IIS 5. 0 and ASP. NET IIS 5. 0 ASP. NET aspnet_wp. exe Thread Pool Http. Application Pooled Inetinfo. exe Http. Modules Http. Handler aspnet_isapi. dll Http. Handler Application Domain

IIS 6. 0 and ASP. NET IIS 6. 0 ASP. NET w 3 wp.

IIS 6. 0 and ASP. NET IIS 6. 0 ASP. NET w 3 wp. exe aspnet_wp. exe http. sys Http. Handler Application Domain inetinfo. exe

HTTP Pipeline Components Configurable components: HTTP Handler Factories HTTP Handlers HTTP Modules SOAP Extensions

HTTP Pipeline Components Configurable components: HTTP Handler Factories HTTP Handlers HTTP Modules SOAP Extensions Use individually, or combined Configure per application, or globally

Pipeline Components Page Request HTTP Handler/Page HTTP Handler Factory HTTP Modules HTTP Response ASPNET_ISAPI.

Pipeline Components Page Request HTTP Handler/Page HTTP Handler Factory HTTP Modules HTTP Response ASPNET_ISAPI. DLL HTTP Request ASP. NET Runtime IIS

Pipeline Components Web Method Request Web Service Method SOAP Extension HTTP Handler/ Web. Services.

Pipeline Components Web Method Request Web Service Method SOAP Extension HTTP Handler/ Web. Services. Handler HTTP Handler Factory HTTP Modules HTTP Response ASPNET_ISAPI. DLL HTTP Request ASP. NET Runtime IIS

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers SOAP Extensions

HTTP Modules Leverage an event-driven model to interact with Web applications Can interact with

HTTP Modules Leverage an event-driven model to interact with Web applications Can interact with every HTTP request Useful for: Custom authorization Global error handler Implementing caching or other utilities Request diagnostics

HTTP Modules Windows. Authentication. Module Forms. Authentication Passport. Authentication. Module File. Authorization. Module Url.

HTTP Modules Windows. Authentication. Module Forms. Authentication Passport. Authentication. Module File. Authorization. Module Url. Authorization. Module Session. State. Module

HTTP Modules Configuration Configured in <http. Modules> Instantiated in order configured Intercept events before

HTTP Modules Configuration Configured in <http. Modules> Instantiated in order configured Intercept events before the application object (global. asax) receives them <system. web> … <http. Modules> <add name=“Event. Module" type=“Web. Handlers. Event. Module, Web. Handlers"/> </http. Modules> … </system. web>

HTTP Modules IHttp. Modules implement IHttp. Module Interface IHttp. Module { void Init( Http.

HTTP Modules IHttp. Modules implement IHttp. Module Interface IHttp. Module { void Init( Http. Application context ); void Dispose(); }

HTTP Modules Initialization Register for events during Init() protected void Init() { application. Pre.

HTTP Modules Initialization Register for events during Init() protected void Init() { application. Pre. Request. Handler. Execute += (new Event. Handler(this. Application_Pre. Request. Handler. Execute)); application. Post. Request. Handler. Execute += (new Event. Handler(this. Application_Post. Request. Handler. Execute)); }

HTTP Modules Event Handlers Events based on Event. Handler delegate Access to Http. Application

HTTP Modules Event Handlers Events based on Event. Handler delegate Access to Http. Application object private void Application_Pre. Request. Handler. Execute(Object source, Event. Args e) { Http. Application app = (Http. Application)source; } Can also publish events to listening applications within the module’s scope

HTTP Modules Application Events ASP. NET HTTP Modules Page Resource Begin. Request() Authenticate. Request()

HTTP Modules Application Events ASP. NET HTTP Modules Page Resource Begin. Request() Authenticate. Request() Authorize. Request() Resolve. Request. Cache() Acquire. Request. State() Pre. Request. Handler. Execute() HTTP GET somepage. aspx Post. Request. Handler. Execute() Release. Request. State() Update. Request. Cache() End. Request()

HTTP Module

HTTP Module

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers SOAP Extensions

HTTP Handler Factories & HTTP Handlers Components configured to handle requests for specific resources

HTTP Handler Factories & HTTP Handlers Components configured to handle requests for specific resources HTTP handler factories return an HTTP handler to process the request Or, you can configure the HTTP handler directly Useful for: Intercepting requests for specific resources Overriding how requests are processed Formatting requests for custom types

HTTP Handler Factories System. Web. UI. Page. Handler. Factory System. Web. Services. Protocols. Web.

HTTP Handler Factories System. Web. UI. Page. Handler. Factory System. Web. Services. Protocols. Web. Service. Handler. Factory System. Runtime. Remoting. Channels. Http. Remoting. Handler. Factory

HTTP Handler Factories Configuration Register in <http. Handlers> Can override in application Web. config

HTTP Handler Factories Configuration Register in <http. Handlers> Can override in application Web. config <http. Handlers> <add verb="*" path="*. aspx" type="System. Web. UI. Page. Handler. Factory"/> <add verb="*" path="*. asmx" type="System. Web. Services. Protocols. Web. Service. Handler. Factory, System. Web. Services, …/> </http. Handlers>

HTTP Handler Factories IHttp. Handler. Factory Implement IHttp. Handler. Factory interface IHttp. Handler. Factory

HTTP Handler Factories IHttp. Handler. Factory Implement IHttp. Handler. Factory interface IHttp. Handler. Factory { IHttp. Handler Get. Handler( Http. Context context, string request. Type, string url, string path. Translated ); void Release. Handler( IHttp. Handler handler ); } Get. Handler() Returns IHttp. Handler type Can access Http. Context and related info Release. Handler() Cleanup!

HTTP Handler Factory Page Request ASP. NET HTTP Modules Handler Factory Handler Page Resource

HTTP Handler Factory Page Request ASP. NET HTTP Modules Handler Factory Handler Page Resource Begin. Request() Authenticate. Request() Authorize. Request() Resolve. Request. Cache() Get. Handler() Acquire. Request. State() Pre. Request. Handler. Execute() Process. Request() HTTP GET somepage. aspx Post. Request. Handler. Execute() Release. Request. State() Update. Request. Cache() End. Request()

HTTP Handler Factory Web Service Method ASP. NET HTTP Modules Handler Factory Handler Web

HTTP Handler Factory Web Service Method ASP. NET HTTP Modules Handler Factory Handler Web Service Begin. Request() Authenticate. Request() Authorize. Request() Resolve. Request. Cache() Get. Handler() Acquire. Request. State() Pre. Request. Handler. Execute() Process. Request() Web. Method() Post. Request. Handler. Execute() Release. Request. State() Update. Request. Cache() End. Request()

HTTP Handlers System. Web. Http. Forbidden. Handler System. Web. Static. File. Handler System. Web.

HTTP Handlers System. Web. Http. Forbidden. Handler System. Web. Static. File. Handler System. Web. Http. Method. Not. Allowed. Handler System. Web. Handlers. Trace. Handler System. Web. UI. Page

HTTP Handlers Configuration Can directly configure IHttp. Handler instead of IHttp. Handler. Factory used

HTTP Handlers Configuration Can directly configure IHttp. Handler instead of IHttp. Handler. Factory used when specific handler may vary per request specifics <http. Handlers> <add verb="*" path="trace. axd" type="System. Web. Handlers. Trace. Handler"/> <add verb="*" path="*. config" type="System. Web. Http. Forbidden. Handler"/> <add verb="GET, HEAD" path="*" type="System. Web. Static. File. Handler"/> </http. Handlers>

HTTP Handlers IHttp. Handler Implement IHttp. Handler Interface IHttp. Handler { void Process. Request(Http.

HTTP Handlers IHttp. Handler Implement IHttp. Handler Interface IHttp. Handler { void Process. Request(Http. Context context ); bool Is. Reusable {get; } } Process. Request() Invoked by Http. Runtime Handle the request accordingly Is. Reusable If resource can be shared, return true

HTTP Handler Factories & Handlers

HTTP Handler Factories & Handlers

HTTP Handlers Accessing Session To access Session from a custom handler, implement marker interface,

HTTP Handlers Accessing Session To access Session from a custom handler, implement marker interface, IRequires. Session. State class Custom. Handler: IHttp. Handler, IRequires. Session. State { public void Process. Request(Http. Context context ) { object somd. Data = context. Session[“data”]; … } public bool Is. Reusable { get { return true; } } }

HTTP Handlers Asynchronous design pattern Offloads request to a new thread Frees thread from

HTTP Handlers Asynchronous design pattern Offloads request to a new thread Frees thread from the application thread pool for better performance Limited gains without scalable architecture interface IHttp. Async. Handler : IHttp. Handler { IAsync. Result Begin. Process. Request( Http. Context context, Async. Callback cb, object extra. Data ); void End. Process. Request(IAsync. Result result ); }

HTTP Handlers *. ASHX Handlers may be defined in an. ashx file Flexible, lightweight

HTTP Handlers *. ASHX Handlers may be defined in an. ashx file Flexible, lightweight implementation No IIS configuration required <add verb="*" path="*. ashx" type="System. Web. UI. Simple. Handler. Factory" /> With or without code-behind <%@ Web. Handler Language="C#" Class=“My. Custom. Handler" %> public class My. Custom. Handler: IHttp. Handler {…}

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers

Agenda IIS & ASP. NET Configuration HTTP Modules HTTP Handler Factories and HTTP Handlers SOAP Extensions

Message Serialization ASP. NET uses Xml. Serializer to serialize/deserialize SOAP messages SOAP Client Proxy

Message Serialization ASP. NET uses Xml. Serializer to serialize/deserialize SOAP messages SOAP Client Proxy Serialize Request Deserialize Web Method XML Parameters Return Value Parameters Deserialize SOAP Response Serialize Return Value XML Client Application Web Service

SOAP Extensions Web Service Method SOAP Extensions Validate/Transform Authorize/Authenticate Log Decrypt Decompress HTTP Response

SOAP Extensions Web Service Method SOAP Extensions Validate/Transform Authorize/Authenticate Log Decrypt Decompress HTTP Response ASPNET_ISAPI. DLL HTTP Request ASP. NET Runtime IIS

SOAP Extensions Provide a mechanism to interact with processing Web service messages Specifically serialization

SOAP Extensions Provide a mechanism to interact with processing Web service messages Specifically serialization and deserialization For example: Encyrypt/decrypt SOAP messages Transform messages before/after serialization/deserialization processes Log requests

Web. Service. Handler Base class to Web service handlers Web. Service. Handler. Factory handles

Web. Service. Handler Base class to Web service handlers Web. Service. Handler. Factory handles delegation to correct handler Web. Service. Handler Sync. Sessionless. Handler Sync. Session. Handler Async. Sessionless. Handler Async. Session. Handler

SOAP Extension Serialization/Deserialization ASP. NET HTTP Modules Handler Factory Handler SOAP Extension Web Service

SOAP Extension Serialization/Deserialization ASP. NET HTTP Modules Handler Factory Handler SOAP Extension Web Service Begin. Request() Authenticate. Request() Authorize. Request() Resolve. Request. Cache() Get. Handler() Acquire. Request. State() Pre. Request. Handler. Execute() Process. Request() Before. Deserialize After. Deserialize Web. Method() Before. Serialize Post. Request. Handler. Execute() Release. Request. State() Update. Request. Cache() End. Request() After. Serialize

SOAP Extension Implementation Create an extension class that derives from Soap. Extension Create attribute

SOAP Extension Implementation Create an extension class that derives from Soap. Extension Create attribute class that derives from Soap. Extension. Attribute Configure the extension: For entire Web service in Web. config For specific method using extension attribute

Soap. Extension Class Create a custom extension class, extend Soap. Extension Class Soap. Extension

Soap. Extension Class Create a custom extension class, extend Soap. Extension Class Soap. Extension { public abstract object Get. Initializer( Type service. Type ); public abstract object Get. Initializer( Logical. Method. Info method. Info, Soap. Extension. Attribute attribute ); public abstract void Initialize( object initializer ); public virtual Stream Chain. Stream( Stream stream ); public abstract void Process. Message( Soap. Message message ); }

SOAP Extension Workflow ASP. NET Once per application SOAP Extension Web Service Get. Initializer()

SOAP Extension Workflow ASP. NET Once per application SOAP Extension Web Service Get. Initializer() Initialize() Chain. Stream() Process. Message() – Before Deserialize Each method request Process. Message() – After Deserialize [Web. Method] Chain. Stream() Process. Message() – Before Serialize Process. Message() – After Serialize

Process. Message() Access to Soap. Message at various serialization stages SOAP extensions on the

Process. Message() Access to Soap. Message at various serialization stages SOAP extensions on the Web server: On request, message is deserialized Soap. Message. Stage. Before. Deserialize Soap. Message. Stage. After. Deserialize On response, message is serialized: Soap. Message. Stage. Before. Serialize Soap. Message. Stage. After. Serialize

Process. Message() SOAP extensions on the Web client: On request, message is serialized Soap.

Process. Message() SOAP extensions on the Web client: On request, message is serialized Soap. Message. Stage. Before. Serialize Soap. Message. Stage. After. Serialize On response, message is deserialized: Soap. Message. Stage. Before. Deserialize Soap. Message. Stage. After. Deserialize

Chain. Stream() Provides access to the memory buffer of the SOAP request Do not

Chain. Stream() Provides access to the memory buffer of the SOAP request Do not have to override this, default behavior returns original stream Can return a new stream object for ASP. NET to reference public override Stream Chain. Stream( Stream stream ) { m_old. Stream = stream; m_new. Stream = new Memory. Stream(); return m_new. Stream; }

SOAP Extension Configuration Using XML configuration to invoke extension for all services within configuration

SOAP Extension Configuration Using XML configuration to invoke extension for all services within configuration scope <web. Services> <soap. Extension. Types> <add type=“Web. Handlers. Log. Extension, Log. Extension" priority="0" group="0" /> </soap. Extension. Types> </web. Services> Configure using Soap. Header. Attribute for individual methods

SOAP Extensions

SOAP Extensions

Session Summary You learned about. NET support for configuring the HTTP pipeline You’ve seen

Session Summary You learned about. NET support for configuring the HTTP pipeline You’ve seen some examples employing HTTP modules, handler factories and handlers You’ve seen where SOAP extensions fit in the pipeline for Web services

For More Information. NET Dashboard Resources http: //www. dotnetdashboard. net/Sessions/handlers. aspx http: //www. dotnetdashboard.

For More Information. NET Dashboard Resources http: //www. dotnetdashboard. net/Sessions/handlers. aspx http: //www. dotnetdashboard. net/Sessions/soapext. aspx IDesign Downloads http: //www. idesign. net

Attend a free chat or web cast http: //www. microsoft. com/communities/chats/default. mspx http: //www.

Attend a free chat or web cast http: //www. microsoft. com/communities/chats/default. mspx http: //www. microsoft. com/usa/webcasts/default. asp List of newsgroups http: //communities 2. microsoft. com/communities/newsgroups/en-us/default. aspx MS Community Sites http: //www. microsoft. com/communities/default. mspx Locate Local User Groups http: //www. microsoft. com/communities/usergroups/default. mspx Community sites http: //www. microsoft. com/communities/related/default. mspx

Please fill out a session evaluation on Comm. Net Q 1: Overall satisfaction with

Please fill out a session evaluation on Comm. Net Q 1: Overall satisfaction with the session Q 2: Usefulness of the information Q 3: Presenter’s knowledge of the subject Q 4: Presenter’s presentation skills Q 5: Effectiveness of the presentation

Questions?

Questions?

© 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only.

© 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.