Web Server and ASP.NET Application
Life Cycle in Depth IIS6 and IIS 7
A Web Server (like Internet Information
Server/Apache/etc.) is a piece of software that enables a website to be viewed
using HTTP. We can abstract this concept by saying that a web
server is a piece of software that allows resources (web pages, images, etc.)
to be requested over the HTTP protocol. I am sure many of you have
thought that a web server is just a special super computer, but it's just the
software that runs on it that makes the difference between a normal computer
and a web server.
As everyone knows, in a Web Communication, there are two
main actors: the Client and the Server.
The client and the server, of course, need a connection to
be able to communicate with each other, and a common set of rules to be able to
understand each other. The rules they need to communicate are called protocols.
Conceptually, when we speak to someone, we are using a protocol. The protocols
in human communication are rules about appearance, speaking, listening, and
understanding. These rules, also called protocols of conversation,
represent different layers of communication. They work together to help people
communicate successfully. The need for protocols also applies to computing
systems. A communications protocol is a formal description of
digital message formats and the rules for exchanging those messages in or
between computing systems and in telecommunications.
HTTP knows all the "grammar", but it doesn't know anything
about how to send a message or open a connection. That's why HTTP is built on
top of TCP/IP. Below, you can see the conceptual model of this protocol on top
of the HTTP protocol:
TCP/IP is in charge of managing the connection and
all the low level operations needed to deliver the message exchanged between
the client and the server.
In this article, I won't explain how TCP/IP works, because I should
write a whole article on it, but it's good to know it is the engine that allows
the client and the server to have message exchanges.
HTTP is a connectionless protocol,
but it doesn't mean that the client and the server don't need to establish a
connection before they start to communicate with each other. But, it means that
the client and the server don't need to have any prearrangements before they
start to communicate.
Connectionless means the client doesn't care if the server is ready to
accept a request, and on the other hand, the server doesn't care if
the client is ready to get the response, but a connection is still needed.
In connection-oriented communication, the communicating peers must first
establish a logical or physical data channel or connection in a dialog
preceding the exchange of user data.
Now, let's see what happens when a user puts an address into the browser
address bar.
- The
browser breaks the URL into three parts:
- The
protocol ("HTTP")
- The
server name (www.Pelusoft.co.uk)
- The
file name (index.html)
- The
browser communicates with a name server(DNS) to translate the server name
"www.Pelusoft.co.uk" into an IP address, which it uses to
connect to the server machine.
- The
browser then forms a connection to the server at that IP address on port
80. (We'll discuss ports later in this article.)
- Following
the HTTP protocol, the browser sents a GET request to the
server, asking for the file "http://www.pelusoft.co.uk.com/index.htm".
(Note that cookies may be sent from the browser to the server with the GET request --
see How Internet Cookies Work for details.)
- The
server then sents the HTML text for the web page to the browser. Cookies
may also be sent from the server to the browser in the header for the
page.)
- The
browser reads the HTML tags and formats the page onto your screen.
- The protocol ("HTTP")
- The server name (www.Pelusoft.co.uk)
- The file name (index.html)
The current practice requires that the connection be
established by the client prior to each request, and closed by the
server after sending the response. Both clients and servers should be aware
that either party may close the connection prematurely, due to user action,
automated time-out, or program failure, and should handle such closing in a
predictable fashion. In any case, the closing of the connection by either or
both parties always terminates the current request, regardless of
its status.
At this point, you should have an idea about how the HTTP -
TCP/IP protocol works. Of course, there is a lot more to say, but the
scope of this article is just a very high view of these protocols just to
better understand all the steps that occur since the user starts to browse a
web site.
Now it's time to go ahead, moving the focus on to what happens when the
web server receives the request and how it can get the request itself.
As I showed earlier, a web server is a "normal computer" that
is running a special software that makes it a Web Server. Let's suppose that
IIS runs on our web server. From a very high view, IIS is just a process which
is listening on a particular port (usually 80). Listening means it is ready to
accept a connections from clients on port 80. A very important thing to
remember is: IIS is not ASP.NET. This means that IIS doesn't know
anything about ASP.NET; it can work by itself. We can have a web
server that is hosting just HTML pages or images or any other kind of web
resource. The web server, as I explained earlier, has to just return the
resource the browser is asking for.
Old Browser is using Serial connection HandShaking Mechanism
Modern Browsers using parallel connection Handshaking mechanism
Modern browsers are using Persistent Connection mechanism in Modern browsers
HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair.
ASP.NET and IIS
The web server can also support server scripting (as ASP.NET).
What I show in this paragraph is what happens on the server running ASP.NET and
how IIS can "talk" with the ASP.NET engine. When we
install ASP.NET on a server, the installation updates the
script map of an application to the corresponding ISAPI extensions to process
therequest given to IIS. For example, the "aspx"
extension will be mapped to aspnet_isapi.dll and hence requests
for anaspx page to IIS will be given to aspnet_isapi (the ASP.NET registration
can also be done using Aspnet_regiis). The script map is shown
below:
The ISAPI filter is a plug-in that can access an
HTTP data stream before IIS gets to see it. Without the ISAPI filter, IIS can
not redirect a request to the ASP.NET engine
(in the case of a .aspx page). From a very high point of view,
we can think of the ISAPI filter as a router for IIS requests:
every time there is a resource requested whose file extension is
present on the map table (the one shown earlier), it redirect the request to
the right place. In the case of an .aspx page, it redirects
the request to the .NET runtime that knows how to elaborate
the request.
When a request comes in:
- IIS creates/runs the work processor (w3wp.exe) if it is not running.
- The aspnet_isapi.dll is hosted in the w3wp.exe process. IIS checks for the script map and routes the requestto aspnet_isapi.dll.
- The request is passed to the .NET runtime that is hosted into w3wp.exe as well.
Finally, the request gets
into the ASP.NET runtime
This paragraph focuses on how the runtime handles the request and
shows all the objects involved in the process.
First of all, let's have a look at what happens when the request gets
to the runtime.
- When ASP.NET receives the first request for any resource in an application, a class named Application Manager creates an Application domain. (Application domains provide isolation between applications for global variables, and allow each application to be unloaded separately.)
- Within an application domain, an instance of the class named Hosting Environment is created, which provides access to information about the application such as the name of the folder where the application is stored.
- After the application domain has been created and the Hosting Environment object instantiated, ASP.NET creates and initializes core objects such as HttpContext, HttpRequest, and HttpResponse.
- After all core application objects have been initialized, the application is started by creating an instance of the HttpApplication class.
- If the application has a Global.asax file, ASP.NET instead creates an instance of the Global.asax class that is derived from the HttpApplication class and uses the derived class to represent the application.
Those are the first steps that happens against a client request.
Most articles don't say anything about these steps. In this article, we will
analyze in depth what happens at each step.
Below, you can see all the steps the request has to
pass though before it is elaborated.
Application Manager
The
first object we have to talk about is the Application
Manager. Application Manager is actually an object that sits on top of
all running ASP.NET AppDomains,
and can do things like shut them all down or check for idle status. For
example, when you change the configuration file of your web
application, the Application Manager is in charge to restart the
AppDomain to
allow all the running application instances (your web site instance) to
be
created again for loading the new configuration file you may have
changed.
Requests that are already in the pipeline processing will
continue to run through the existing pipeline, while any newrequest coming
in gets routed to the new AppDomain. To avoid the problem of "hung requests", ASP.NET forcefully
shuts down the AppDomain after the request timeout period is
up, even if requests are still pending.
Application Manager is the "manager", but the Hosting Environment
contains all the "logic" to manage the application instances. It's
like when you have a class that uses an interface: within the class methods,
you just call the interface method. In this case, the methods are called within
the Application Manager, but are executed in the Hosting Environment (let's
suppose the Hosting Environment is the class that implements the interface).
At this point, you should have a question: how is it possible the
Application Manager can communicate with the Hosting Environment since it lives
in an AppDomain? (We said the AppDomain creates a kind of boundary around the
application to isolate the application itself.) In fact, the Hosting
Environment has to inherit from the MarshalByRefObject class to use
Remoting to communicate with the Application Manager. The Application Manager
creates a remote object (the Hosting Environment) and calls methods on it.
So we can say the Hosting Environment is the "remote
interface" that is used by the Application Manager, but the code is
"executed" within the Hosting Environment object.
Application Manager
The
first object we have to talk about is the Application
Manager. Application Manager is actually an object that sits on top of
all running ASP.NET AppDomains,
and can do things like shut them all down or check for idle status. For
example, when you change the configuration file of your web
application, the Application Manager is in charge to restart the
AppDomain to
allow all the running application instances (your web site instance) to
be
created again for loading the new configuration file you may have
changed.
Requests that are already in the pipeline processing will
continue to run through the existing pipeline, while any newrequest coming
in gets routed to the new AppDomain. To avoid the problem of "hung requests", ASP.NET forcefully
shuts down the AppDomain after the request timeout period is
up, even if requests are still pending.
Application Manager is the "manager", but the Hosting Environment
contains all the "logic" to manage the application instances. It's
like when you have a class that uses an interface: within the class methods,
you just call the interface method. In this case, the methods are called within
the Application Manager, but are executed in the Hosting Environment (let's
suppose the Hosting Environment is the class that implements the interface).
At this point, you should have a question: how is it possible the
Application Manager can communicate with the Hosting Environment since it lives
in an AppDomain? (We said the AppDomain creates a kind of boundary around the
application to isolate the application itself.) In fact, the Hosting
Environment has to inherit from the MarshalByRefObject class to use
Remoting to communicate with the Application Manager. The Application Manager
creates a remote object (the Hosting Environment) and calls methods on it.
So we can say the Hosting Environment is the "remote
interface" that is used by the Application Manager, but the code is
"executed" within the Hosting Environment object.
No comments:
Post a Comment