Database SQL Oracle MySql
Tuesday, March 11, 2014

ASP.NET Request Processing Pipeline

ASP.NET Request Processing Pipeline
Web application basically is a client-server web application. On the client side, this application is hosted by a browser, such as Internet Explorer. The application's user interface takes the form of Hypertext Markup Language (HTML) pages that are interpreted and displayed by the client's browser.
On the server side, the Web application runs under Microsoft Internet Information Services (IIS). IIS manages the application, passes requests from clients to the application, and returns the application's responses to the client. These requests and responses are passed across the Internet using Hypertext Transport Protocol (HTTP).  The following figure shows how the client and server interact.



Figure1: Client Server Architecture
When a user types a URL in the address bar of the browser he makes a request to the server, server receives the request, process the request through a HTTP pipeline object model and send response back to the browser.
We can divide the whole process into 4 steps
1.       The browser makes an HTTP request for an asp.net web page.
2.       The web server receives the HTTP request.
3.       Process the Request
4.       Response to the browser
These four steps are elaborated below.
1.       The browser makes an HTTP request for an asp.net web page
The process begins with this step. Suppose our BankUltimus project is hosted as ultimus in server which has an IP address 114.129.10.58.You type http://114.129.10.58/abc/login.aspx in browser’s address bar. The browser then makes a HTTP request to the BankUltimus application server.
2.       The web server receives the HTTP request
The whole task of the web server is to receive HTTP request and return requested resource in an HTTP response. Microsoft's Internet Information Services (IIS) Web server receives the request. The first thing IIS does when a request comes in is decide how to handle the request. Its decision is based upon the requested file's extension. For example, if the requested file has the .asp extension, IIS will route the request to be handled by asp.dll.

There are numerous file extensions that map to the ASP.NET engine, some of which include:
Ø   .aspx, for ASP.NET Web pages,
Ø   .asmx, for ASP.NET Web services,
Ø   .config, for ASP.NET configuration files,
Ø   .ashx, for custom ASP.NET HTTP handlers,
Ø   .rem, for remoting resources,
Ø   Others!


Figure 2: IIS Administration

In the IIS administration, you can configure the extension mappings. Above screen shot is mapping file extension and handler. You can add your own custom extensions here. That is, you can have requests for .leads files routed to ASP.NET Engine. In case of MVC application we map ‘.*’ extension to aspnet_isapi.dll which indicate all request will pass through this dll first.
The diagram below illustrates the steps 1 and 2 of a request for an ASP.NET Web page. When a request comes into the Web server, it is routed to the proper place (asp.dll for classic ASP page requests, ASP.NET engine for ASP.NET requests) based on the requested file's extension.




Figure 3: Receiving request from client
3.       Process the Request
We can divide the processing of request in two steps
3.1.  Create ASP.Net environment for serving the request
3.2.  Request processing using Module, Handler and ASP.Net page events.
3.1. Creation of ASP.Net environment for serving the request
Creation of ASP.Net environment is done by the following steps
i.                     If this the first request to the website then a class called as ‘ApplicationManager’ creates an application domain where the website can run. As we all know application domain creates isolation between two web applications hosted on the same IIS. So in case there is issue in one application domain it does not affect the other application domain. If this is not the first request in the website then it directly goes to step 3.

ii.                   The newly created application domain creates hosting environment i.e. the HttpRuntime object.

iii.                  Once the hosting environment is created necessary core ASP.NET objects -- HttpContext, HttpRequest and HttpResponse objects are created.


iv.                 In case of first request to the website HttpApplication object is created to serve the request. If you have a global.asax  file in your system then object of the global.asax file will be created. Please note global.asax file inherits from HttpApplication class.

v.                   If this is not first request then HttpApplication is taken from pool
vi.                 The HttpApplication object is then assigned to the core ASP.NET objects to process the page.

vii.                HttpApplication then starts processing the request by http module events , handlers and page events.
Creation of asp.net environment can be described by the following figure



Figure 4: Creation of ASP.Net environment
Following image explains how the internal object model looks like for an ASP.NET request. At the top level is the ASP.NET runtime which has creates an Application domain which in turn has HttpRuntime with request, response and context objects.



Figure 5:  Internal objects of ASP.Net request
3.2. Request processing using Module, Handler and ASP.Net page events
After creation of HTTPApplication object it starts request processing. Then it goes through number of HTTPModules. After this it goes to HttpHandlers which process the request. Page events are also fired at this time. Developers can add customize the events also create their own Modules and Handlers.
HTTP Modules: HTTP Modules are the classes that have access to the incoming request. These modules can inspect the incoming request and make decisions that affect the internal flow of the request.
There are a number of pre-built HTTP modules that are included in the HTTP pipeline by default. These modules include:
Ø  OutputCache, which handles returning and caching the page's HTML output, if needed
Ø  Session, which loads in the session state based on the user's incoming request and the session method specified in the Web.config file
Ø  FormsAuthentication, which attempts to authenticate the user based on the forms authentication scheme, if used
Ø   And others!

In fact, you can see a precise list of what modules are used by default by going to the machine.config file
and searching for the <httpModules> element. Followings are the default <httpModules> element:

<httpModules>
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" />
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule,
System.Web.Mobile, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</httpModules>

HTTP Handlers: HTTP Handlers are the endpoint of HTTP pipeline and it processes the request and generates response. For ASP.NET Web pages, this means rendering the Web controls into HTML and returning this HTML. For a Web service, it would involve executing the specified method and wrapping its return values into an appropriately formatted SOAP response.

Different ASP.NET resources use different HTTP handlers. The handlers used by default are spelled out in the machine.config's <httpHandler> section.

Followings are the <httpHandler>elements in machine.config
<httpHandlers>
<add verb="*" path="*.vjsproj" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.java" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.jsl" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="trace.axd" type="System.Web.Handlers.TraceHandler" />
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
<add verb="*" path="*.ashx" type="System.Web.UI.SimpleHandlerFactory" />
</httpHandlers>

You can create your own HTTP modules and HTTP handlers, and then plug them into the pipeline for all Web sites on the Web server by modifying machine.config, or you can add them to a particular Web application by modifying that application's Web.config file.

Difference between HTTP Handlers and HTTP Modules

If you want to inject logic based in file extensions like ‘.ASPX’ , ‘.HTML’ then you use HttpHandler. In other words HttpHandler is an extension based processor.




Figure 6:  HttpHandlers

If you want to inject logic in the events of ASP.NET pipeline then you use HttpModule. In other word HttpModule is an event based processor.


Figure 7: HttpModule events
Request Processing Steps using HTTP Handlers, HTTP Modules and Page events
Step 1 ( HttpModule): There are 6 important events which you can utilize before your page object is created BeginRequest’, AuthenticateRequest, AuthorizeRequest, ResolveRequestCache, AcquireRequestState and PreRequestHandlerExecute. You can add customize logic also in these events.
Step 2 (HttpHandler): Once the above 6 events are fired, ASP.NET engine will invoke ProcessRequest event if you have implemented HttpHandler in your project.

Step 3 (ASP.NET page): After executing HttpHandler logics page object is created. In that time many events are fired which can help us two implement our customized logics. The main events sequence can be called SILVER U (Start, Init, Load, Validation, Event, Render, and Unload)

Step 4 (HttpModule): Once the page object is executed and unloaded from memory HttpModule provides post page execution events. There are 4 important post-processing events PostRequestHandlerExecute, ReleaseRequestState, UpdateRequestCache and EndRequest. You can add your customize logic also in this events

4.       Response to the browser
HttpHandler generate appropriate output (in HTML format). This output is then passed back through HttpModules and then back to IIS. At last IIS sends it back to the client which instantiated the request.  The response is returned as HTML code. Browser receives the HTML and displays it.
Whole process can be pictured by the following figure.


Figure 8: ASP.Net request processing.


Description of the events
Following table shows in which event what kind of logic or code can go.
Section
Event
Description
HttpModule
BeginRequest
This event signals a new request; it is guaranteed to be raised on each request.
HttpModule
AuthenticateRequest

This event signals that ASP.NET runtime is ready to authenticate the user.

HttpModule
AuthorizeRequest
This event signals that ASP.NET runtime is ready to authorize the user.
HttpModule
ResolveRequestCache

In ASP.NET we normally use outputcache directive to do caching.  In this event ASP.NET runtime determines if the page can be served from the cache rather than loading the patch from scratch.
HttpModule
AcquireRequestState

This event signals that ASP.NET runtime is ready to acquire session variables.
HttpModule
PreRequestHandlerExecute

This event is raised just prior to handling control to the HttpHandler. Before you want the control to be handed over to the handler any pre-processing you would like to do.
HttpHandler
ProcessRequest
Httphandler logic is executed.
ASP.NET Page
SILVERU
Refer to ASP.NET Page life cycle documents
HttpModule
PostRequestHandlerExecute
Any logic you would like to inject after the handlers are executed.
HttpModule
ReleaserequestState
If save update some state variables like session variables is required that is done here.
HttpModule
UpdateRequestCache
Before you end if cache is required to update then update cache.
HttpModule
EndRequest
This is the last stage before your output is sent to the client browser.

Table 1: ASP.NET request processing pipeline events
Glossary
HTTP Request: HTTP stands for HyperText Transfer Protocol.  HTTP is connectionless request response protocol. When a user write a URL in address bar of browser he/she actually request for a page which is called HTTP request.
HTTP Response: In answer to the HTTP request server process the request and return response to the browser as HTML code which is called HTTP response.
ASP.NET Engine: ASP.NET engine consists of all the required components for processing HTTP request
Application Server: Application server is the sever in which application is hosted.
Global.asax: It is a global file for all users. It consists of several application and session level events such as Application_BeginRequest, Application_Error, Session_Start, Session_End, and so on.
Aspnet_isapi.dll: HTTP request is first passed to this dll.
Web.config: It’s a configurable file. Items that is changeable (example: connection string) is kept in this file. It is application specific.
Machine.config: It’s also configurable file. It is machine specific i.e. it is common for applications in a machine.


  • Blogger Comment
  • Facebook Comment

0 comments:

Copyright © 2014 ASP.NET & C# & IIS & Crystal Report & Database & ADO.NET All Right Reserved
Designed by ASP.NET Tuts