Http pipeline

38
Http Pipeline

Transcript of Http pipeline

Page 1: Http pipeline

Http Pipeline

Page 2: Http pipeline

Agenda

● Revisit Process & Thread● Http Pipeline Modes● Http Pipeline Objects● Http Pipeline Extensibility

Page 3: Http pipeline

Thread, Process and AppDomain

Page 4: Http pipeline

Threads in a Process

OS has many processes and a process has many threads

Processes are resource intensive

OS runs processes in isolations mode to provide security, reliability and availability

IPC is slow

Page 5: Http pipeline

AppDomains in a Process

IIS Webserver is a Process

Process is overloaded if apps are hosted

Error in a app brings down entire Web Server processApplication Domains provide security, reliability and availability

CLR controls AppDomains and they are isolated

Each .NET application runs under AppDomain

Failure of an app does not impact other apps

Vipul patel
Isolated AppDomain: CLR allocates unique memory addresses to an AppDomain that ensures AppDomains don’t access others’ memory.
Page 6: Http pipeline

Pipeline ModesClassic Mode - IIS5+Integrated Mode - IIS7+

Page 7: Http pipeline

Classic Mode

pipe

inetinfo.exe

Vipul patel
nternet Server Application Programming Interface (ISAPI)
Vipul patel
http://www.iis.net/learn/application-frameworks/building-and-running-aspnet-applications/aspnet-integration-with-iis
Page 8: Http pipeline

Classic Mode

It only works with ISAPI extensions and ISAPI filters.

ASP.NET modules can’t control/extend certain areas of IIS request execution like inetinfo.exe.

ASP.NET featuers like modules/handlers/etc are not available for Non-ASP.NET apps.

IIS treats ASP.NET as an external plugin instead of its part.

Vipul patel
IIS just treats ASP.NET as an external plugin implemented in ISAPI and works with it like a black box (and only when it's needs to give out the request to ASP.NET). In this mode, ASP.NET is not much different from PHP or other technologies for IIS.
Vipul patel
IIS 7 follows ASP.net integration model.What happen here this model gives ability to plug in directly into the server pipeline allows ASP.NET modules to replace, run before, or run after any IIS 7 functionality. IIS 7 processes requests that arrive for any content type, with both native IIS modules and ASP.NET modules providing request processing in all stages.This enables services that are provided by ASP.NETmodules, such as Forms authentication or output cache, to be used for requests to ASP pages, PHP pages, static files, and so on.
Page 9: Http pipeline

Integrated Mode

Vipul patel
http://msdn.microsoft.com/en-us/magazine/cc188942.aspx
Page 10: Http pipeline

Pipeline Object Model

Page 11: Http pipeline

Pipeline - Inside

Page 12: Http pipeline

HttpContext - Intro

Instance per request

Request specific data

Application specific data

Accessible in different layers including Handlers and Modules

Page 13: Http pipeline

HttpContext - Properties

Page 14: Http pipeline

Hands On

HttpContext - Accessing its properties in business classHttpContext - Generating Text Response

Page 15: Http pipeline

Homework

HttpContext - Creating CookiesHttpContext - Generating csv file

Page 16: Http pipeline

Pipeline ExtensibilityHttpApplication

Modules

Handlers

Page 17: Http pipeline

HttpApplication - Global.asax

Entry class of a request

Repository of globally resources like Application, Session, Cache

Contains events of application and page lifecycle

Using global.asax, default behavior of application/request can be changed

global.asax is optional

Any change on global.asax raises signal to restart an application

Page 18: Http pipeline

HttpApplication - Members

Page 19: Http pipeline

HttpApplication - Events

Page 20: Http pipeline

HttpApplication - Overridable Methods

Page 21: Http pipeline

HttpApplication - Advance

Application_OnStart and Application_OnEnd are raised only once during an application’s lifetimeRequest, Response, and Session properties are not available in Init and Dispose methodsEvents must be declared with following pattern: Application_EventName

global.asax file can handle events published by any modules in the request and events must be declared with following pattern: FriendlyModuleName_EventName. Session_OnStart and Session_OnEnd are part of Session module and defined in global.asax to cleanup session values.

Page 22: Http pipeline

Hands On

HttpApplication - Request/Response Time

Page 23: Http pipeline

Handlers

Handlers are classes that implements IHttpHandler interface and configured in web.config

Handlers are .ashx file declared with @WebHandler directive - It automatically calls already registered SimpleHandlerFactory

Handlers are .aspx file declared with @Page directive - It automatically calls with already registered PageHandlerFactory

In Classic mode [IIS 6 and earlier], URI path must be mapped for aspnet_isapi.dll

Handlers generates customized responses

Common Handlers are .aspx, .ashx, .asmx

Page 24: Http pipeline

Handlers - Use Cases

Non-mapped handlers can respond to any file name extension

Use Async Handler that depends on external services because thread is not blocked and threadpool able to accept more requests.

Customized response that basically does not contain HTML like RSS feed

On demand, content manipulations like Image resize

Vipul patel
When you create an asynchronous handler, you must implement the IHttpAsyncHandler interface. You must also implement the BeginProcessRequest method in order to initiate an asynchronous call that processes individual HTTP requests. In addition, you must implement the EndProcessRequest method to run cleanup code when the process ends.
Vipul patel
Asynchronous HTTP handlers enable you to start an external process, such as a method call to a remote server. The handler can then continue processing without waiting for the external process to finish. While an asynchronous HTTP handler is processing, ASP.NET puts the thread that would ordinarily be used for the external process back into the thread pool until the handler receives a callback from the external process. This can prevent thread blocking and can improve performance because only a limited number of threads can execute at the same time. If many users request synchronous HTTP handlers that rely on external processes, the operating system can run out of threads, because many threads are blocked and are waiting for an external process.
Page 25: Http pipeline

Handlers - Implementation

IsReusable = true, HttpHandlerFactory pools those handler objects that improves performanceIsReusable = false, HttpHandlerFactory always creates new handler instanceIsReusable = true, not recommended if your handler generates response based on user/request typeBy Default, IsReusable = false for standard handlers like .aspx, .asmx, .ashx

Page 26: Http pipeline

Handlers - Classic Mode Configuration

Page 27: Http pipeline

Handlers - Integrated Mode Configuration

Page 28: Http pipeline

HandlerFactory - Customization

Handler Factories are classes that implements IHttpHandlerFactory interface and configured in web.config just like handlersFactories controls on processing requests by creating different handlers on runtime conditions.HandlerFactory configuration is equivalent to Handler configuration.

HandlerFactory receives requests and forward requests to appropriate handlers

Vipul patel
For example, with a custom HTTP handler factory, you can instantiate one HTTP handler for a file type if the HTTP request method is PUT, and another if the method is GET.
Page 29: Http pipeline

Hands On

Http Handler - ConfigHttp Handler - ashxHttp Handler - aspx

Page 30: Http pipeline

Homework

Http Handler - AsyncHandlerFactory - change handler based on condition

Page 31: Http pipeline

Modules

Modules are classes that implements IHttpModule interface and configured in web.configModules are application specific while Handlers are extension specific. A module is invoked for all requests/responses

Modules are called on every request as part of request pipelineModules have access to life cycle events throughout the requestModules examine requests and perform appropriate actions. They can allow to modify responseA few ASP.NET modules are Session, Caching, Forms Authentication.

Page 32: Http pipeline

Modules - Use Cases

Custom headers/footersRewrite Requests - basically rewrite urls

Security as Modules are called for all requestsStats and Logging

Page 33: Http pipeline

Module - Implementation

Modules are always pooled

Modules can be deployed at machine level. High reusability compared to global.asax

Page 34: Http pipeline

Modules - Classic Mode Configuration

Page 35: Http pipeline

Modules - Integrated Mode Configuration

Page 36: Http pipeline

Hands On

Http Module - Ip Address RestrictionHttp Module - Request Logging

Page 37: Http pipeline

Homework

HttpModule - Common FooterHttpModule - Rewrite Url

Page 38: Http pipeline

Questions