Application Request Response – ASP.NET
In ASP.NET to process requests a pipeline model is used which consists of HTTPContext , HTTPModules and HTTPHandler objects. This pipeline model forms the low level framework which is used by web pages and web services. Here we will see high level overview of asp.net request processing.
ISAPI(Internet Server Application Programming Interface)
The user sends a request to IIS.IIS first checks which ISAPI(Internet Server Application Programming Interface) extension can serve this request. Depending on file extension the request is processed and check the request is First Request or Not . If First Request then Worker Process will Created , If No then reuse the existing Worker Process.
If this is the first request to the website, then Worker Process(W3WP.exe) creates a class called as ApplicationManager which creates an app domain where the website can run.
The newly created app domain creates hosting environment, i.e. the HttpRuntime object. Once the hosting environment is created its create HttpWorkerRequest and HttpApplicationFactrory.
Then HttpApplicationFactrory maintains an HttpApplication-Object pool to service Requests for the Application. Pool lasts only as long as the Application. HttpApplicationFactrory then creates HttpContext.
Once HttpConext created then all the core ASP.NET objects like HttpRequest , HttpResponse , HttpSessionState , HttpApplicationState will created.
- HttpRequest : An HTTP client sends an HTTP request to a server in the form of a request message
- HttpResponse: After receiving and interpreting a request message, a server responds with an HTTP response message
- HttpSessionState: Session state information is stored on the server and not on the client side. When the session expires, this session information is removed completely and automatically. You can change the session expiration duration through web.config file. Session data is unique for each user. You can always use it using ajax or change it or even remove it.
- HttpApplicationState: HttpApplicationState is used to store application data that typically does not change. It is important to note that Application State is a singleton and is not thread safe. So when you make changes to it, you must lock and unlock the Application object via calls to Application.Lock() and Application.UnLock().
Once all the core ASP.NET objects are created, then HttpApplicationFactrory will check If this is the first request then it will create HttpApplication object to serve the request otherwise it will take HttpApplication from Pool. In case you have a global.asax file in your system, then the object of the “global.asax” file will be created.
HttpApplication then starts processing the request by HTTP module.
HttpModule is an event based processor to inject pre-processing logic before after the request reaches the Page. ASP.NET uses HTTP Module to implement lots of its own functionality like authentication and authorization, session management and output caching etc.
There are 6 important events(HttpModule)
- BeginRequest: This event signals a new request; it is guaranteed to be raised on each request.
- AuthenticateRequest : This event signals that ASP.NET runtime is ready to authenticate the user. Any authentication code can be injected here.
- AuthorizeRequest : This event signals that ASP.NET runtime is ready to authorize the user. Any authorization code can be injected here.
- ResolveRequestCache : In this event, ASP.NET runtime determines if the page can be served from the cache rather than loading the patch from scratch.
- AcquireRequestState : This event signals that ASP.NET runtime is ready to acquire session variables.
- PreRequestHandlerExecute : This event is raised just prior to handling control to the HttpHandler.
Once the HttpModule 6 events are fired, ASP.NET engine will invoke Process Request event and fires HandlerFactory which invoke HttpHandler.
HTTP handler is the process (frequently referred to as the “endpoint”) that runs in response to a request made to an ASP.NET Web application.
Some ASP.NET default handlers are:
- Page Handler (.aspx) – Handles Web pages
- User Control Handler (.ascx) – Handles Web user control pages
- Web Service Handler (.asmx) – Handles Web service pages
- Trace Handler (trace.axd) – Handles trace functionality
Once the HttpHandler logic executes, the ASP.NET page object is created.Page Life Cycle.
- Control Events
- Load Complete
HttpModule Post Execution
Once the page object is executed and unloaded from memory, HttpModule provides post page execution events which can be used to inject custom post-processing logic.
- PostRequestHandlerExecute : Any logic you would like to inject after the handlers are executed.
- ReleaserequestState : If you would like to save update some state variables like sessionvariables.·
- UpdateRequestCache : Before you end, if you want to update your cache.·
- EndRequest : This is the last stage before your output is sent to the client browser.