38
Http Pipeline

Http pipeline

Embed Size (px)

Citation preview

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 process

Application 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

Page 6: Http pipeline

Pipeline Modes

Classic Mode - IIS5+

Integrated Mode - IIS7+

Page 7: Http pipeline

Classic Mode

pipe

inetinfo.exe

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.

Page 9: Http pipeline

Integrated Mode

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 class

HttpContext - Generating Text Response

Page 15: Http pipeline

Homework

HttpContext - Creating Cookies

HttpContext - Generating csv file

Page 16: Http pipeline

Pipeline Extensibility

HttpApplication

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 lifetime

Request, Response, and Session properties are not available in Init and Dispose methods

Events 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

Page 25: Http pipeline

Handlers - Implementation

IsReusable = true, HttpHandlerFactory pools those handler objects that improves performance

IsReusable = false, HttpHandlerFactory always creates new handler instance

IsReusable = true, not recommended if your handler generates response based on user/request

type

By 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 handlers

Factories 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

Page 29: Http pipeline

Hands On

Http Handler - Config

Http Handler - ashx

Http Handler - aspx

Page 30: Http pipeline

Homework

Http Handler - Async

HandlerFactory - change handler based on condition

Page 31: Http pipeline

Modules

Modules are classes that implements IHttpModule interface and configured in web.config

Modules 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 pipeline

Modules have access to life cycle events throughout the request

Modules examine requests and perform appropriate actions. They can allow to modify response

A few ASP.NET modules are Session, Caching, Forms Authentication.

Page 32: Http pipeline

Modules - Use Cases

Custom headers/footers

Rewrite Requests - basically rewrite urls

Security as Modules are called for all requests

Stats 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 Restriction

Http Module - Request Logging

Page 37: Http pipeline

Homework

HttpModule - Common Footer

HttpModule - Rewrite Url

Page 38: Http pipeline

Questions