KEMBAR78
Http pipeline | PPTX
Http Pipeline
Agenda
โ— Revisit Process & Thread
โ— Http Pipeline Modes
โ— Http Pipeline Objects
โ— Http Pipeline Extensibility
Thread, Process and AppDomain
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
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
Pipeline Modes
Classic Mode - IIS5+
Integrated Mode - IIS7+
Classic Mode
pipe
inetinfo.exe
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.
Integrated Mode
Pipeline Object Model
Pipeline - Inside
HttpContext - Intro
Instance per request
Request specific data
Application specific data
Accessible in different layers including Handlers and Modules
HttpContext - Properties
Hands On
HttpContext - Accessing its properties in business class
HttpContext - Generating Text Response
Homework
HttpContext - Creating Cookies
HttpContext - Generating csv file
Pipeline Extensibility
HttpApplication
Modules
Handlers
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
HttpApplication - Members
HttpApplication - Events
HttpApplication - Overridable Methods
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.
Hands On
HttpApplication - Request/Response Time
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
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
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
Handlers - Classic Mode Configuration
Handlers - Integrated Mode Configuration
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
Hands On
Http Handler - Config
Http Handler - ashx
Http Handler - aspx
Homework
Http Handler - Async
HandlerFactory - change handler based on condition
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.
Modules - Use Cases
Custom headers/footers
Rewrite Requests - basically rewrite urls
Security as Modules are called for all requests
Stats and Logging
Module - Implementation
Modules are always pooled
Modules can be deployed at machine level. High reusability compared to global.asax
Modules - Classic Mode Configuration
Modules - Integrated Mode Configuration
Hands On
Http Module - Ip Address Restriction
Http Module - Request Logging
Homework
HttpModule - Common Footer
HttpModule - Rewrite Url
Questions

Http pipeline

  • 1.
  • 2.
    Agenda โ— Revisit Process& Thread โ— Http Pipeline Modes โ— Http Pipeline Objects โ— Http Pipeline Extensibility
  • 3.
  • 4.
    Threads in aProcess 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
  • 5.
    AppDomains in aProcess 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
  • 6.
    Pipeline Modes Classic Mode- IIS5+ Integrated Mode - IIS7+
  • 7.
  • 8.
    Classic Mode It onlyworks 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.
  • 9.
  • 10.
  • 11.
  • 12.
    HttpContext - Intro Instanceper request Request specific data Application specific data Accessible in different layers including Handlers and Modules
  • 13.
  • 14.
    Hands On HttpContext -Accessing its properties in business class HttpContext - Generating Text Response
  • 15.
    Homework HttpContext - CreatingCookies HttpContext - Generating csv file
  • 16.
  • 17.
    HttpApplication - Global.asax Entryclass 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
  • 18.
  • 19.
  • 20.
  • 21.
    HttpApplication - Advance Application_OnStartand 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.
  • 22.
    Hands On HttpApplication -Request/Response Time
  • 23.
    Handlers Handlers are classesthat 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
  • 24.
    Handlers - UseCases 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
  • 25.
    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
  • 26.
    Handlers - ClassicMode Configuration
  • 27.
    Handlers - IntegratedMode Configuration
  • 28.
    HandlerFactory - Customization HandlerFactories 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
  • 29.
    Hands On Http Handler- Config Http Handler - ashx Http Handler - aspx
  • 30.
    Homework Http Handler -Async HandlerFactory - change handler based on condition
  • 31.
    Modules Modules are classesthat 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.
  • 32.
    Modules - UseCases Custom headers/footers Rewrite Requests - basically rewrite urls Security as Modules are called for all requests Stats and Logging
  • 33.
    Module - Implementation Modulesare always pooled Modules can be deployed at machine level. High reusability compared to global.asax
  • 34.
    Modules - ClassicMode Configuration
  • 35.
    Modules - IntegratedMode Configuration
  • 36.
    Hands On Http Module- Ip Address Restriction Http Module - Request Logging
  • 37.
    Homework HttpModule - CommonFooter HttpModule - Rewrite Url
  • 38.

Editor's Notes

  • #10ย The following list describes the request-processing flow that is shown in Figure 1: When a client browser initiates an HTTP request for a resource on the Web server, HTTP.sys intercepts the request. HTTP.sys contacts WAS to obtain information from the configuration store. WAS requests configuration information from the configuration store, applicationHost.config. The WWW Service receives configuration information, such as application pool and site configuration. The WWW Service uses the configuration information to configure HTTP.sys. WAS starts a worker process for the application pool to which the request was made. The worker process processes the request and returns a response to HTTP.sys. The client receives a response.
  • #12ย from its pages. It captures the true essence of ASP.NET and walks the reader to a high level of technical and architectural skill."-J. Fred Maples, Director of Software Engineering, NASDAQ.com Essential ASP.NET with Examples in C# is the C# programmer's definitive reference for ASP.NET through version 1.1. It provides experienced programmers with the information needed to fully understand the technology, and is a clear guide to using ASP.NET to build robust and well architected Web applications. This book begins with a discussion of the rationale behind the design of ASP.NET and an introduction to how it builds on top of the .NET framework. Subsequent chapters explore the host of new features in ASP.NET, including the server-side compilation model, code-behind classes, server-side controls, form validation, the data binding model, and custom control development. Throughout the book, working examples illustrate best practices for building Web-based applications in C#. Among the topics explored in depth are: ASP.NET architecture Web forms Configuration HTTP pipeline Diagnostics and error handling Validation Data binding Custom controls Caching State management The first thing that happens when a request is dispatched to an application is that an instance of the HttpWorkerRequest class is created (1), which contains all the information about the current request, including the requested URL, the headers, and so on. Once the HttpWorkerRequest class is created, it is passed into the static ProcessRequest method of the HttpRuntime class, which is executed in the AppDomain of the application, initiating the processing of the request (2). The first thing the HttpRuntime class does is to create a new instance of the HttpContext class, initialized with the HttpWorkerRequest class (3). The HttpContext class is the "glue" of the pipeline, since it holds all the classes together by keeping all the relevant information about the current request in one location. When the HttpContext class is first created, it allocates new instances of the HttpRequest and HttpResponse classes and stores them as fields. It also provides property accessors to the application and session state bags. Once the HttpContext class is created, the HttpRuntime class requests an instance of the HttpApplicationderived class for this application by calling the static GetApplicationInstance method of the HttpApplicationFactory class (4). GetApplicationInstance either creates a new instance of the HttpApplication (or a derivative) class or pulls one from a pool of application objects if one is available (5). Once the HttpApplication class is created or retrieved, it is initialized, and during its initialization it allocates any modules that are defined for this application (6). Modules are classes that implement the IHttpModule interface and serve to pre- and postprocess requests. Once the modules have been created, the HttpRuntime class asks its newly retrieved HttpApplication class to service the current request by calling its BeginProcessRequest method (7), defined by the IHttpAsyncHandler interface implemented by the application class. The HttpApplication class then takes over the request processing and locates the appropriate handler factory for the current request, based on the URL path. For example, if the request is for an .aspx page, it uses the PageHandlerFactory class. Once it locates the appropriate factory, it invokes the GetHandler method on the IHttpHandlerFactory interface to retrieve a fresh copy of the appropriate handler class. Handler classes serve as the endpoint for requests and very often are simply the Page-derived class that is created from an .aspx file. In general, handlers are classes that implement the IHttpHandler interface and populate the response buffer when asked to process a request. Once the handler is created, its ProcessRequest method is called (8), passing in the current HttpContext class so that it has access to the Request, the Response, and all the other request-specific pieces of information necessary. Once the ProcessRequest method returns, the request is complete