`
renzhelife
  • 浏览: 669092 次
文章分类
社区版块
存档分类
最新评论

ASP.NET Internals – IIS and the Process Model (IIS5, IIS6)

 
阅读更多

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.NETis no longer something which resembles its predecessor.ASP.NETis 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,JSPandASP, 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.NETis 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 howIISdoes 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 theHTTPprotocol. 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 whereISAPIextensions appear, whereISAPIstands for Internet Server ApplicationProgrammingInterface.ISAPIextensions are modules implemented as plain old Win32 .dll, on whichIISrelies to process specific resources. Mappings betweenISAPIextensions and files are configured via theIISsnap-in and stored in theIISmetabase, where each file extension can be associated with a particularISAPIextension, that is, when a request for such a file arrives,IIShandles it to the correspondingISAPIextension, confident that it will be able to handle it.

Figure 1: Configuring ISAPI extensions mappings on IIS 5.0

ISAPIextensions obviously need to respect a common interface through which they can be called byIISand provided the necessary data to elaborate the request and generate a response.

As Figure 1 illustrates, the .asp extension is mapped to theasp.dllISAPIextension; at the time ofASPthis component was in charge of performing all the tasks required to generate a response, that is, collecting information about the request, made available into theASPpage via the Request, Response and other commonASPintrinsic objects, parsing and executing theASPpage and returning the resulting HTML.

Actually, that was a big improvement compared to a technology like CGI, butASP.NETtakes this approach much further and introduces abstractions which totally shield the developers from having to care about what happens at this stage.

Wheninstalled,ASP.NETconfiguresIISto redirect requests forASP.NETspecific files to a newISAPIextension calledaspnet_isapi.dll. What this extension does is somewhat different then the formerasp.dllextension, which was essentially responsible just for parsing and executing the requestedASPpage. The steps taken by a genericISAPImodule to process a request are totally hidden fromIIS, thereforeISAPIextension 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.NETISAPIextension 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.NETfile is picked up byIIS, it is passed to theaspnet_isapi.dll, which is the main entry point forASP.NETrelated processing. Actually, what theISAPIextension does depends sensibly on the version ofIISavailable on the system, and thus the process model, which is the sequence of operations performed by theASP.NETruntime to process the request and generate a response, may vary quite a bit.

When running underIIS5.X, all ASP.NET-related requests are dispatched by theISAPIextension to an external worker process calledaspnet_wp.exe. TheASP.NETISAPIextension, hosted in theIISprocessinetinfo.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.NETworker process performs a considerable number of tasks, together with theISAPIextension. They are the main authors of all the stuff that happens under the hoods of anASP.NETrequest. 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.NETworker process. To provide isolation and abstraction from the execution context theASP.NETmodel introduces the concept of Application Domains, in brief AppDomains. They can be considered as lightweight processes. More on this later.

If running underIIS6, on the other side, theaspnet_wp.exeprocess is not used, in favour of another process calledw3wp.exe. Furthermore,inetinfo.exeis no longer used to forwardHTTPrequests toISAPIextensions, 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, althoughIIS6 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 ofIIS5, is that incoming requests are in the former handled at a lower – Kernel – level and then forwarded to the correctISAPIextension, 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 theIISinetinfo.exeprocess listening by default on the TCP port 80 for incomingHTTPrequests and queuing them into a single queue, waiting to be processed. If the request is specific toASP.NET, the processing is delegated to theASP.NETISAPIextension,aspnet_isapi.dll. This, in turn, communicates with theASP.NETworker process,aspnet_wp.exevia named pipes and finally is the worker process which takes care of delivering the request to theASP.NETHTTPruntime 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.NETHTTPRuntime 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 theHTTPRuntime Environment can be considered as a black box where all theASP.NETspecific 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.NETPipeline orHTTPRuntime pipeline.

One of the interesting points of this process model is that all the requests, once handled by theISAPIextension, are passed to theASP.NETworker process. Only one instance of this process is active at a time, with one exception, discussed later. Therefore allASP.NETweb applications hosted onIISare 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.NETintroduces the concept of AppDomain, which is essentially a sort of managed lightweight process which provides isolation and security boundaries. EachIISvirtual 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.NETpipeline 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, theISAPIextension 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

TheIIS6 process model is the default model on machines running Windows 2003 Server operating system. It introduces several changes and improvements over theIIS5 process model. One of the biggest changes is the concept of application pools. OnIIS5.X all web applications, that is, all AppDomains, were hosted by theASP.NETworker process. To achieve a finer granularity over security boundaries and personalization, theIIS6 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 wayIISlistens for incoming requests. With theIIS5 model, it was theIISprocess,inetinfo.exe, who was listening on a specific TCP port forHTTPrequests. In theIIS6 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 inIIS5 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.NETISAPIextension, which, in turn, loads the CRL and delegates all the work to theHTTPRuntime.

Thew3wp.exeworker process, differently from theaspnet_wp.exeprocess used inIIS5 model, isn’tASP.NETspecific, and is used to handle any kind of requests. The specific worker process then decides whichISAPImodules 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 inIIS6 called Web Administration Service (WAS). This module is responsible for reading worker process – web application bindings from theIISmetabase and forwarding the request to the right worker process.

References


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics