Introduction
Microsoft ActiveServerPages, also known asASP
, since its first release in late 1996 provided web developers with a rich and complex framework for building web applications. As years passed its infrastructure evolved and improved so much that what is now known asASP.NET
is no longer something which resembles its predecessor.ASP.NET
is a framework for building web applications, that is, applications that run over the web, where the client-server paradigm is represented mostly by a browser forwarding requests for resources of different kinds to a web server. Before the advent of dynamic server-side resource generation techniques likeCGI
,PHP
,JSP
andASP
, all web servers had to do was accept client’s requests for static resources and forward them to the requestor. When dynamic technologies started to grow up web servers became invested of greater responsibility, since they had to find a way to generate those dynamic resources on their side and return the result to the client, a task they were not formerly built for.
From a bird’s eye view, the interaction between client and server is very simple. Communications over the web occur viaHTTP
(Hyper Text Transfer Protocol), an application level protocol which relies on TCP and IP to transmit data between two nodes connected to the heterogeneous network known as World Wide Web.
Each dynamic server-side technology essentially leans on a particular web server implementation, andASP.NET
is tightly coupled with Microsoft’s InternetInformationServer, akaIIS
.
Different servers chose different ways to generate and serve dynamic resources and what we’re going to examine is howIIS
does that, together with the path a request follows once on the server and back to the client.
IIS and ISAPI Extensions
As mentioned, static resources needn’t to be processed by the server; once a request for such a resource arrives, the server just retrieves its contents from thefilesystem and sends it back to the client as a stream of byte flowing on theHTTP
protocol. Static resources can be images,Javascriptfiles, css style sheets or plain old html pages. It’s clear that the server needs to know how to distinguish between static and dynamic resource, whereas the second need to be processed somehow and not just sent back to the client. That’s whereISAPI
extensions appear, whereISAPI
stands for Internet Server ApplicationProgrammingInterface.ISAPI
extensions are modules implemented as plain old Win32 .dll, on whichIIS
relies to process specific resources. Mappings betweenISAPI
extensions and files are configured via theIIS
snap-in and stored in theIIS
metabase, where each file extension can be associated with a particularISAPI
extension, that is, when a request for such a file arrives,IIS
handles it to the correspondingISAPI
extension, confident that it will be able to handle it.
Figure 1: Configuring ISAPI extensions mappings on IIS 5.0
ISAPI
extensions obviously need to respect a common interface through which they can be called byIIS
and provided the necessary data to elaborate the request and generate a response.
As Figure 1 illustrates, the .asp extension is mapped to theasp.dll
ISAPI
extension; at the time ofASP
this component was in charge of performing all the tasks required to generate a response, that is, collecting information about the request, made available into theASP
page via the Request, Response and other commonASP
intrinsic objects, parsing and executing theASP
page and returning the resulting HTML.
Actually, that was a big improvement compared to a technology like CGI, butASP.NET
takes this approach much further and introduces abstractions which totally shield the developers from having to care about what happens at this stage.
Wheninstalled,ASP.NET
configuresIIS
to redirect requests forASP.NET
specific files to a newISAPI
extension calledaspnet_isapi.dll
. What this extension does is somewhat different then the formerasp.dll
extension, which was essentially responsible just for parsing and executing the requestedASP
page. The steps taken by a genericISAPI
module to process a request are totally hidden fromIIS
, thereforeISAPI
extension may follow different paradigms in order to process requests.
Table 1: IIS Application Mappings for aspnet_isapi.dll
Extension | Resource Type |
.asax | ASP.NET application files. Usually global.asax. |
.ascx | ASP.NET user control files. |
.ashx | HTTP handlers, the managed counterpart of ISAPI extensions. |
.asmx | ASP.NET web services. |
.aspx | ASP.NET web pages. |
.axd | ASP.NET internal HTTP handlers. |
As well as the file extensions listed in Table 1, theASP.NET
ISAPI
extension manages other file extensions which are usually not served to web browsers, like Visual Studio project files, source code files and config files, for example.
The ASP.NET Process Model
So far we’ve seen that when a request for anASP.NET
file is picked up byIIS
, it is passed to theaspnet_isapi.dll
, which is the main entry point forASP.NET
related processing. Actually, what theISAPI
extension does depends sensibly on the version ofIIS
available on the system, and thus the process model, which is the sequence of operations performed by theASP.NET
runtime to process the request and generate a response, may vary quite a bit.
When running underIIS
5.X, all ASP.NET-related requests are dispatched by theISAPI
extension to an external worker process calledaspnet_wp.exe
. TheASP.NET
ISAPI
extension, hosted in theIIS
processinetinfo.exe
, passes the control toaspnet_wp.exe
, along with all the information concerning the incoming request. The communication between the two is performed via named pipes, a well known mechanism for IPC (Inter Process Communication). TheASP.NET
worker process performs a considerable number of tasks, together with theISAPI
extension. They are the main authors of all the stuff that happens under the hoods of anASP.NET
request. To introduce a topic which will be discussed later, take note of the fact that each web application, corresponding to a different virtual directory hosted onIIS
, is executed in the context of the same process, theASP.NET
worker process. To provide isolation and abstraction from the execution context theASP.NET
model introduces the concept of Application Domains, in brief AppDomains. They can be considered as lightweight processes. More on this later.
If running underIIS
6, on the other side, theaspnet_wp.exe
process is not used, in favour of another process calledw3wp.exe
. Furthermore,inetinfo.exe
is no longer used to forwardHTTP
requests toISAPI
extensions, although it keeps running for serving other protocols requests. A lot of other details change compared to the process model used by previous versions ofIIS
, althoughIIS
6 is capable of running in compatibility mode and emulate the behavior of its predecessor. A big step forward, compared to the process model used when running on top ofIIS
5, is that incoming requests are in the former handled at a lower – Kernel – level and then forwarded to the correctISAPI
extension, thereby avoiding inter process communication techniques which may represent an expensive operation under a performance and resource consumption point of view. We’ll delve deeper into this topic in the following paragraphs.
IIS 5.0 Process Model
This is the default process model available on Windows 2000 and XP machines. As mentioned it consists in theIIS
inetinfo.exe
process listening by default on the TCP port 80 for incomingHTTP
requests and queuing them into a single queue, waiting to be processed. If the request is specific toASP.NET
, the processing is delegated to theASP.NET
ISAPI
extension,aspnet_isapi.dll
. This, in turn, communicates with theASP.NET
worker process,aspnet_wp.exe
via named pipes and finally is the worker process which takes care of delivering the request to theASP.NET
HTTP
runtime environment. This process is graphically represented in Figure 2.
Figure 2: The IIS 5.0 Process Model
In Figure 2 is represented an additional element we didn’t mention yet, theASP.NET
HTTP
Runtime Environment. It’s not topic of this article and will eventually be explained in a follow up article, but for the sake of this discussion theHTTP
Runtime Environment can be considered as a black box where all theASP.NET
specific processing takes place, all the managed code lives and developers can actually put their hands on, from the HttpRuntime straight to the HttpHandler who will finally process the request and generate the response. This is even referred to as theASP.NET
Pipeline orHTTP
Runtime pipeline.
One of the interesting points of this process model is that all the requests, once handled by theISAPI
extension, are passed to theASP.NET
worker process. Only one instance of this process is active at a time, with one exception, discussed later. Therefore allASP.NET
web applications hosted onIIS
are actually hosted inside the worker process, too. However, this doesn’t mean that all the applications are run under the same context and share all their data. As mentioned,ASP.NET
introduces the concept of AppDomain, which is essentially a sort of managed lightweight process which provides isolation and security boundaries. EachIIS
virtual directory is executed in a single AppDomain, which is loaded automatically into the worker process whenever a resource belonging to that application is requested for the first time. Once the AppDomain is loaded – that is, all the assemblies required to satisfy that request are loaded into the AppDomain – the control is actually passed to theASP.NET
pipeline for the actual processing. Multiple AppDomains can thus run under the same process, while requests for the same AppDomain can be served by multiple threads. However, a thread doesn’t belong to an AppDomain and can serve requests for different AppDomains, but at a given time a thread belongs to a single AppDomain.
For performance purposes the worker process can be recycled according to some criteria which can be specified declaratively in the machine.config file placed in the directory C:\windows\microsoft.net\Framework\[framework version]\CONFIG. These criteria are the age of the process, number of requests served and queued, time spent idle and consumed memory. Once one of the threshold value of these parameters is reached, theISAPI
extension creates a new instance of the worker process, which will we used from then on to serve the requests. This is the only time when multiple copies of the process can be running concurrently. In fact, the old instance of the process isn’t killed, but it is allowed to terminate serving the pending requests.
IIS 6.0 Process Model
TheIIS
6 process model is the default model on machines running Windows 2003 Server operating system. It introduces several changes and improvements over theIIS
5 process model. One of the biggest changes is the concept of application pools. OnIIS
5.X all web applications, that is, all AppDomains, were hosted by theASP.NET
worker process. To achieve a finer granularity over security boundaries and personalization, theIIS
6 process model allows applications to run inside different copies of a new worker process,w3wp.exe
. Each application pool can contain multiple AppDomains and is hosted in a single copy of the worker process. In other words, the shift is from a single process hosting all applications to multiple processes hosting each an application pool. This model is also called the workerprocessisolationmode.
Another big change from the previous model is the wayIIS
listens for incoming requests. With theIIS
5 model, it was theIIS
process,inetinfo.exe
, who was listening on a specific TCP port forHTTP
requests. In theIIS
6 architecture, incoming requests are handled and queued at kernel level instead of user mode via a kernel driver called http.sys; this approach has several advantages over the old model and is called kernel-level request queuing.
Figure 3: The IIS 6.0 process model
Figure 3 illustrates the principal components taking part in the request processing when using the II 6 model. Once a request arrives the kernel level device driver http.sys routes it to the right application pool queue. Each queue belongs to a specific application pool, and thus to a specific copy of the worker process, which next receives the request from the queue. This approach highly reduces the overhead introduced by named pipes used inIIS
5 model since no inter process communication is taking place, but the requests are headed to the worker process directly from the kernel level driver. This has many advantages concerning reliability, too. Since running in kernel mode, the request dispatching isn’t influenced by crashes and malfunctions happing at user level, that is, in the worker processes. Thereby, even if a worker process crashes, the system is still capable of accepting incoming requests and eventually restarts the crashed process.
It’s the worker process who is in charge of loading theASP.NET
ISAPI
extension, which, in turn, loads the CRL and delegates all the work to theHTTP
Runtime.
Thew3wp.exe
worker process, differently from theaspnet_wp.exe
process used inIIS
5 model, isn’tASP.NET
specific, and is used to handle any kind of requests. The specific worker process then decides whichISAPI
modules to load according to the type of resources it needs to serve.
A detail not underlined in Figure 3 for simplicity reasons is that incoming requests are forwarded from the application pool queue to the right worker process via a module loaded inIIS
6 called Web Administration Service (WAS). This module is responsible for reading worker process – web application bindings from theIIS
metabase and forwarding the request to the right worker process.