原文地址:http://blogs.msdn.com/b/wenlong/archive/2006/11/22/virtual-application-vs-virtual-directory.aspx。
People are always confused by these two IIS concepts, especially for the new IIS7/WAS. These are not new concepts. They are available since IIS6 on Windows 2003 Server. However the terms were misused in IIS6 and they are corrected in IIS7 in Windows Vista and higher versions of Windows.
The term “virtual application” is a fundamental concept for IIS and ASP.NET. Here is the definition in my own words: a virtual application is a construction unit of a web site that participates protocol listening and process management. It can be uniquely identified by its absolute virtual path from that web site. For example, you can create a virtual application with virtual path “/foo” in the “Default Web Site”. For simplicity, we also call virtual application as "Default Web Site/foo" or simply “/foo”. Every web site has at least one virtual application, a.k.a, the root virtual application “/”.
A web site can be uniquely identified by its site ID. All of the web sites are under the same root “/W3SVC”. So each web site can be represented in the format of “/W3SVC/<SiteID>”. For example, the “Default Web Site” can be represented as “/W3SVC/1”. With this, we can uniquely identify a virtual application with its full path, for example,
/W3SVC/1/Root/foo
If you have the IIS6 scripting tool “adsutil.vbs” installed, you would be able to check the settings of the virtual application with the following command:
cscript %systemdrive%\inetpub\adminscripts\adsutil.vbs enum /W3SVC/1/Root/foo
When you create a virtual application, you would expect your new virtual application to receive requests that are dedicated to it. There are two aspects of “protocol listening”:
The other aspect of a virtual application is that it also participates in process management. IIS/WAS has the concept of application pools. An application pool is a definition or template of a worker process (w3wp.exe) that is used by IIS/WAS to create new worker process instances. Two most important properties of an application pool are:
Each virtual application must be assigned to an application pool in IIS. All of the requests (except for static content) will be handled in worker processes in that application pool.
Please note that Application Pool is managed by WAS instead of IIS in the IIS7 family. This separation makes non-HTTP activation possible. With this refactoring, “IIS” is simply the web server that only handles HTTP/HTTPS requests.
For ASP.NET, a virtual application has its real meaning as an “application”. When an ASP.NET request is first received in a virtual application, a new AppDomain is created in the worker process that handles subsequent ASP.NET requests. This includes all WCF requests. So in the ASP.NET world, a virtual application is mapped to an AppDomain. This provides important code isolation for managed applications that sits in the same worker process. It is even more important for web hosting when the applications are running in the partial trust environment.
Isn’t “virtual directory” the same as “virtual application”? In IIS6, from the IIS Manager (UI), if you right click on “Default Web Site” -> New, you will see “Virtual Directory …” from the list. What is going on there? Well, as I said, the terms are misused in IIS6. When it says “Virtual Directory”, it actually means “Virtual Application”.
Am I kidding? No. A real “virtual directory” is just a container of files. It is a construction unit of a virtual application, just like a virtual application to a web site. A virtual application must have at least one virtual directory. Here is a sample configuration section that defines the virtual application “/foo” and its virtual directories in the WAS' configuration file %windir%\system32\inetsrv\config\applicationhost.config:
<sites>
<site name="Default Web Site" id="1">
<application path="/foo" enabledProtocols="http">
<virtualDirectory path="/" physicalPath="c:\inetpub\foo" />
<virtualDirectory path="/bar" physicalPath="\\myremote1\bar" />
</application>
...
</site>
</sites>
Note that the virtual directory “/bar” points to a different physical path, actually it points to a network share (UNC) path!
How can we create a real virtual directory in IIS6 then? It is a little bit trickier from the UI. In the IIS Manager UI, you can right click on the virtual application and then click on New -> “Virtual Directory …”. After specifying the alias and path, you will get the virtual directory using all default settings.
However, there is another trick to convert a virtual application into a virtual directory. Here are the steps:
Now you have created a virtual directory “/bar” under the virtual application “/foo”. The icon for the virtual application is different than that of the virtual application. Here is what you will see:
Virtual applications and virtual directories can be nested, meaning, a virtual application or directory can be created under another virtual application or directory in any combinations.
This nesting has the following implications:
Other than those, virtual applications still have their own isolation that I mentioned in the above sections.