Showing posts with label IIS7. Show all posts
Showing posts with label IIS7. Show all posts

Friday, November 28, 2008

What is IIS7?

What is IIS7? What is the difference between IIS6 and IIS7? Here are the simple answers:

§ IIS7 is a new revision (version 7.0) of the Internet Information Services that is shipped on Windows Vista and the next Windows Server version.

§ The most attractive features of IIS7 are: modular design (thanks to the new Windows componentization technology first introduced in Windows Vista), extensible architecture (with public web server APIs), and unified distributed configuration system.

§ Another big change to IIS7 is that it first introduced a new NT service “Windows Process Activation Service” (WAS). WAS manages application pool configuration and worker processes. This enables process activation through both HTTP and non-HTTP transports.

More details about IIS7 can be found in the IIS7 public web site:http://www.iis.net/.

When people asked me how to host WCF services on IIS7? I would always say: it is the same as on IIS6. Well, this is generally correct. People still kept on hitting unique problems on IIS7. I will expand this in more details in my future blog entries.

IIS7/WAS Installation

IIS7 has a modular design which allows you to selectively install components that are needed. This design is based on the new manifest-driven Componentization technology that was first introduced in Windows Vista. As the IIS site claimed, there are more than 40 standalone feature components of IIS7 which can be independently installed. This is good or bad. The good part is that IT professionals can easily customize the installation as needed. The bad part is that it’s hard for developers to understand which components are needed. Here I would like to write something regarding the latter.

If you are familiar with CMI (Component Management Interface) or CBS (Component Based Setup), you would know that there are many different ways to install components on Vista. You can use the GUI interface (called Software Explorer) which is available from “Control Panel” -> Programs -> “Turn Windows features on or off”. Or you can use command-line tools such as pkgmgr.exe or ocsetup.exe (which is a wrapper of pkgmgr.exe). I will talk about the simplest approach, GUI interface, below.

Minimal Installation: Installing WAS

The minimal installation of the whole IIS7 package is to install the Windows Process Activation Service (WAS). WAS is a standalone feature and it is the only feature from the IIS7 family that is available for all Windows Vista client SKUs (Basic, Home, Business, and Ultimate, etc).

From the “Turn Windows features on or off” UI of the control panel, you will see the WAS component listed in the list as in the following image:

This feature has three sub-components:

  • .NET Environment
  • Configuration APIs
  • Process Model

If you select the root node of WAS, only the “Process Model” sub-node is checked to be installed by default with the root. Please note that with this installation, you still cannot do anything interesting today. It only means that you can get WAS to work.

If you further check the “Internet Information Services” feature from the feature list, you will find out that the “Configuration APIs’ node is automatically checked.

In order to make WCF or any ASP.NET application to work, you need to further check the sub-node “.NET Environment”. This means that all of WAS components are needed in order to make WCF/ASP.NET to work well. Fortunately these are automatically checked once you install any of those components.

IIS7: Default Installation

By simply checking the “Internet Information Services” feature from the UI, you will see that quite some sub-nodes are automatically marked as following:

This is the default installation of IIS7. In this way, you can use IIS7 to service static content (such as HTML pages etc). However, you can’t run ASP.NET or CGI applications.

IIS7: Installlation with ASP.NET support

From the above image, it’s obvious that you need to install “ASP.NET” to make ASP.NET work on IIS7. After checking “ASP.NET”, you will find the following UI:

This is the minimal environment for both WCF and ASP.NET applications to work in IIS7.

Installation with IIS6 Compatibility Components

Many people still have problems with the above installation when they run Visual Studio 2005 or some automation scripts/tools (for example, adsutil.vbs) to configure virtual applications. The main reason is because those tools depend on the legacy IIS6 Metabase API support as well as the legacy IIS6 scripting tools. Here is the UI that shows what are missing from above:

You need to check “IIS 6 Scripting Tools” (which automatically checks other three subnodes of “IIS 6 Management Compatibility”). After that, you will see the following UI:

With this installation, you are good to play with IIS7, ASP.NET, and WCF features and samples available on the web.

Maximum Installation

You may want to play with other components that are not installed by default. To install them, you can simply go through the whole tree of the IIS and check every leaf node. In this way, you will get the full IIS7 feature set installed.

How to redirect a Url in IIS6/IIS7 and in ASP.NET?

Sometimes you want to redirect users request to a different one when you want to retire old ones but still provide backward-compatibility. David Wang has a good summary on different redirects for IIS in his blog entry. Here I just want to add a few points which have helped people to resolve tricky problems.

First of all, as David pointed out, there are two (or three in David’s terms) types of redirects:

· Client-side redirection: the server sends a “302 Redirect” response to the client together with the new location header.

· Server-side redirection: the server rewrites the request with another URL transparently and the client is not aware of anything.

Client-side redirection

It is also called HTTP redirection. You can achieve it in the following ways.

Redirect in IIS6

You can use the HttpRedirect Metabase setting to perform redirection in IIS6. This property can be set to a virtual application, a file, or a virtual directory (a.k.a. web folder). It requires the physical existence of these “source” items. To achieve that, you can use IIS Manager. Here are the steps on how to set it for a file:

· Right click on the file and click on Properties

· Click on “File” tab of the dialog

· Select “A redirection to a URL” radio box

· Type in your new Url, and/or make any other changes, and click OK.

After this, you would have setup the file so that all requests to this file is redirected to the new Url. You can check the Metabase setting with the following command:

cscript.exe %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs get /w3svc/1/Root/foo/p1.aspx/HttpRedirect

If the file does not have any Metabase property pre-defined in the Metabase, you cannot use adsutil.vbs to set this property. But you can set it with the script to a virtual application.

Redirect in IIS7

In IIS7, the concept is similar as that of IIS6. You can use the new command “appcmd.exe” to enable redirection as following:

%windir%\system32\inetsrv\appcmd set config "Default Web Site/foo" -section:system.webServer/httpRedirect -enabled:true -destination:"http://localhost/bar/p1.aspx"

This tells IIS to redirect all request sending to the virtual application “/foo” to “/bar/p1.aspx”. The actual result is that appcmd.exe adds the following section to the web.config file for the application “/foo”:

<system.webServer>

<httpRedirect enabled="true"destination="http://localhost/bar/bbb.aspx" />

system.webServer>

Redirect with ASP.NET

You can also use ASP.NET to implement client-side redirection. If you don’t want ASP.NET pipeline to intercept any such request, you would need to implement the logic in the global.asax file. This is important for WCF applications since WCF intercepts requests from the pipeline for WCF requests. Here is some sample code for a global.asax file:

<%@ Application Language="C#" Debug="true" %>

This approach is more flexible than those provided by IIS:

· It does not require the existence of the physical file.

· It is imperative and thus you can add any custom programming logic to determine whether you want to redirect the request. For example, you can check the query string to see whether it contains the information that requires a redirection.

Server-side redirection

It is sometimes also called Url rewriting or Url aliasing. One of the advantages for server-side redirection is performance because there is no round-trip involved if the redirection is on the same machine.

Redirect with ASP.NET

You can use ASP.NET to implement the server-side redirection with Server.Transfer(path) method.

Orcas SP1 Improvement: Asynchronous WCF HTTP Module/Handler for IIS7 for Better Server Scalability

Introduction

As mentioned in my last blog entry, for IIS-hosted WCF services, WCF holds the worker thread coming from ASP.NET until the whole request is completed to avoid a Denial of Service (DOS) attack. I also mentioned that on Windows 2008 Server, IIS7 has introduced the following registry setting to provide request throttling for all incoming requests:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\2.0.50727.0]

“MaxConcurrentRequestsPerCpu”=dword:0000000c

Based on this, in Orcas SP1 (.NET 3.5 SP1), WCF has implemented the asynchronous HTTP Module/Handler to allow better server scalability for high latency requests.

Asynchronous WCF HTTP Module/Handler

Besides the existing synchronous WCF HTTP Module/Handler types, WCF introduced the asynchronous versions. Together, WCF has the following four types implemented:

· Synchronous Module:

System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

· Asynchronous Module (new):

System.ServiceModel.Activation.ServiceHttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

· Synchronous Handler:

System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

· Asynchronous Handler (new):

System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Asynchronous HTTP Module

The new type ServiceHttpModule implements the System.Web.IHttpModule and it registers the async event HttpApplication.AddOnPostAuthenticateRequestAsync with a pair of async handlers:

static public IAsyncResult BeginProcessRequest(object sender, EventArgs e, AsyncCallback cb, object extraData)

static public void EndProcessRequest(IAsyncResult ar)

When a request comes in, WCF does some validation and returns from BeginProcessRequest immediately after passing the request up to internal processing logic. In this way, the worker process is immediately released.

If the service operation is asynchronous and it is waiting for slow I/O operations, the whole stack does not hold any thread. The only performance concern would be the memory usage to hold objects used to process the request. This allows many concurrent requests to be served by WCF without using a lot of threads and thus greatly improves the scalability of the service. This is very helpful when completing each request takes significant amount of time and many clients are served. In some private testing, WCF can easily support more than 10K concurrent slow requests, which is not possible with the synchronous HttpModule.

Asynchronous HTTP Handler Factory

You might have already noticed that WCF implements the Handler Factory instead of the asynchronous Handler directly. This allows more flexibility in the future to support different types of HTTP Handlers. The ServiceHttpHandlerFactory creates a stateless asynchronous HTTP handler of the following type which implements System.Web.IHttpAsyncHandler:

System.ServiceModel.Activation.ServiceHttpHandler

It implements the pair of asynchronous request handlers:

public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)

public void EndProcessRequest(IAsyncResult result)

Default Installation – Synchronous Version

Though the asynchronous version of HTTP Module/Handler is added in this release, the default installation is still the synchronous version. Why is this?

First of all, you should not use asynchronous version of HTTP Module/Handler on Windows 2003 Server (with IIS6) without knowing the risk. If you have many clients, the service would be easily DOS attacked by too much memory usage due to huge amount of pending requests queued up in WCF transport layer without the throttling inside IIS/ASP.NET.

Because of the limitation on IIS6, WCF did not change the setup to switch to purely asynchronous due to the ramifications between IIS6 and IIS7 for this quick release. This means that in order to use the asynchronous version, you have to perform some manual registration.

Don’t worry, I have provided a private tool to do this registration for you.

WCF Module/Handler Registration Tool

You can find the simple tool called WcfAsyncWebUtil.exe attached in this blog. Here is the usage:

Usage: WcfAsyncWebUtil

Options:

/is

Install WCF Synchronous HTTP module and handler into IIS7 configuration

in Integrated Mode.

/ia

Install WCF Asynchronous HTTP module and handler into IIS7 configuration

in Integrated Mode.

/l

List WCF HTTP module and handler registered into IIS7 configuration in

Integrated Mode.

/t [ ]

Configure the throttling registry MaxConcurrentRequestsPerCpu for the ASP.NET

integrated pipeline to the value . Default is 100. This argument is

used together with /ia only

For example, if you want to register asynchronous HTTP Module/Handler with the ASP.NET throttling to be 1000, you can run the following command:

WcfAsyncWebUtil.exe /ia /t 1000

To list the installed WCF HTTP Module/Handler, you can run:

WcfAsyncWebUtil.exe /l