IIS and WAS Hosting Architecture

How Requests to WCF Services Are Processed

9 Min Read
ITPro Today logo in a gray background | ITPro Today

Exploring WCF Web Services

LANGUAGES:VB.NET | C#

TECHNOLOGIES:ASP.NET 2.0 | .NET 3.0 | WCF

 

IIS and WAS Hosting Architecture

How Requests to WCF Services Are Processed

 

By Michele Leroux Bustamante

 

This column explores the hosting model for WCF services inIIS 6 and IIS 7 with the Windows Activation Service (WAS). If you havequestions about migrating your existing ASMX or WSE Web services to WCF, orquestions regarding WCF as it relates to Web services, please send them to [email protected].

 

In earlier columns I gave you a crash course in creatingWCF services that are hosted in IIS, and provided an overview of the Webservice bindings available for configuring service endpoints. So, assuming youare familiar with creating WCF services and configuring endpoints for thoseservices, you re probably ready to learn the hosting architecture forprocessing messages to services. In this column I ll explain the ServiceHosttype; how message-based activation works for WCF services; review how IIShosting works in general terms; and compare the hosting architecture for IIS 6and IIS 7 with the Windows Activation Service (WAS).

 

For some background, read my earlier asp.netPRO articles ASP.NET and WCF: Meet Your New Web Service from the February 2007 issue, and Choosing the Right Web Services Binding from the March 2007 issue.

 

ServiceHost

For those new to WCF, I ll provide a brief overview of theServiceHost type, which is the center of the hosting story. You can host a WCFservice in a Console application, a Windows application, an NT service, and IIS6 or IIS 7 with the Windows Activation Service (WAS). In all cases, aServiceHost type must be constructed to open the communication channel that processesmessages to the service. That means initializing the ServiceHost with a servicetype so it knows which type to construct when a message arrives for aparticular operation. You must also provide the ServiceHost with at least oneendpoint describing:

  • An address where clients can send messages. ForInternet services this would be an HTTP address.

  • A binding indicating the correct protocols touse to invoke the service. This would most likely be BasicHttpBinding orWSHttpBinding (per the March column).

  • A service contract indicating the set ofoperations to make available at the specified address, over the specifiedprotocols.

 

In a self-hosting environment (non-IIS) you mightinitialize the ServiceHost as follows:

 

ServiceHost host = new

ServiceHost(typeof(HelloIndigo.HelloIndigoService));

host.AddServiceEndpoint(typeof(HelloIndigo.HelloIndigoService)

, new WSHttpBinding(),"http://localhost:8000/HelloIndigoService");

host.Open();

// keep host alive until process shutdown

host.Close();

 

When Open is called, a communication channel isconstructed for the endpoint, along with a message dispatcher. Clients useproxies to invoke service operations at a particular endpoint. When the messagereaches the service model, a service object is constructed (if necessary) tohandle the incoming message. Figure 1 illustrates this flow for a clientcommunicating with a service hosted by a Windows service process.

 


Figure 1: Hosting model for aWindows service.

 

For IIS-hosted services the processing model is the sameonce the message reaches the service model; however, the client sends messagesto a .svc endpoint instead, which in turn is associated with the ServiceHostand service type. Ultimately, the ServiceHost instance lives inside the WWWWorker Process (frequently called the ASP.NET Worker Process), as shown inFigure 2. In this case, no code is required to construct and open theServiceHost type this is handled automatically through message-basedactivation (to be discussed). The .svc file indicates which service type shouldhandle messages targeting the endpoint, as follows:

 

<% @ServiceHostService="HelloIndigo.HelloIndigoService" %>

 


Figure 2: The IIS hosting model froma high level.

 

The point is that in both cases a ServiceHost instancemust be constructed, associated with a service type, expose endpoints for thatservice type, and opened to create communication channels that processmessages.

 

Message-based Activation

Services not hosted in IIS or WAS are called self-hostedservices. This means developers must provide the process and the logic forconstructing the ServiceHost. One drawback of self-hosting is that the hostprocess must be running before requests to the service can be processed (sothat the ServiceHost communication channels are ready to process messages). Anotheris that all messages are funneled to the same host process, so there is noautomatic distribution of load or active health monitoring of availableprocesses.

 

In the case of IIS and WAS hosting, the host process andServiceHost instance are constructed (if necessary) in response to incomingmessages known as message-based activation. From a high level, this meansmessages are queued waiting for an available worker process and ServiceHostinstance to be constructed. In addition, one or more worker processes can beallocated to process messages, increasing throughput and scalability. There arevariations in how this works for IIS 6 and IIS 7/WAS, but the high-level viewconsistent to both hosting environments is shown in Figure 3.

 


Figure 3: Request queues andmessage-based activation.

 

Message-based activation means that idle worker processesand associated ServiceHost instances can be released and reconstructed ondemand preserving precious server resources.

 

IIS 6 Hosting

On a Windows Service 2003 machine you ll use IIS 6 to hostHTTP endpoints for your WCF services. With IIS 6, requests first arrive at thehttp.sys kernel and are ultimately forwarded to a worker process where theServiceHost lives. The WWW Service launches worker processes on demand toprocess requests and makes sure the request is sent to the appropriaterequest queue.

 

IIS 6 should be configured so that incoming requests for.svc files are forwarded to the aspnet_isapi.dll, just like other ASP.NETextensions such as .aspx and .asmx. That means requests to WCF services hostedin IIS are initially processed by the ASP.NET pipeline; however, servicerequests are ultimately handled by the service model, not by ASP.NET.

 

The ASP.NET pipeline looks for an HTTP handler to processeach request but, along the way, HTTP modules that have been loaded into theapplication domain have the opportunity to interact with application eventsbefore and after the handler is invoked. In the case of WCF services, theSystem.ServiceModel.Activation namespace provides two such types:

  • HttpModule.An IHttpModule implementation intended to hijack request processing forservices, to send the request to the service model.

  • HttpHandler.An IHttpHandler implementation that processes requests that are flowed throughthe traditional ASP.NET pipeline, if ASP.NET compatibility mode is enabled.

 

Figure 4 illustrates how IIS 6 processes requests up tothe HttpModule and HttpHandler where the baton is passed to the servicemodel.

 


Figure 4: How IIS 6 and ASP.NETprocess WCF service requests.

 

For the time being, I ll focus on the default processingmodel for WCF services (mixed transport mode). In this mode, although intheory requests for .svc files are destined for the configured HttpHandlertype for processing, the HttpModule hooks the PostAuthenticateRequestapplication event and gets a chance early in the request cycle to redirectprocessing to the service model and bypass the ASP.NET pipeline. DuringPostAuthenticateRequest the HttpModule checks to see if ASP.NET compatibilityis enabled; if not, the request is forwarded to the service model forprocessing via the HttpChannelListener from System.ServiceModel.Channels. Asper Figure 4, the HttpHandler is never invoked in this case. In addition, whenthe module forwards requests to the service model, it releases the ASP.NETthread and transfers control to a thread from the WCF thread pool. That way,other ASP.NET requests can use the ASP.NET thread but more importantly, thisallows WCF to consistently use its own thread pool configuration for allhosting environments.

 

As for ASP.NET compatibility mode, this feature allows WCFservices to be processed by the traditional ASP.NET pipeline, whereby theHttpHandler will ultimately handle the request. In addition, this means thatother configured HTTP modules can interact with the request throughout itslifecycle, such as those for caching, authentication, session, error handling,profiles, and others. Eventually the HttpHandler will facilitate processing therequest (executing the operation) in the service model way, and operations willhave access to the HttpContext among other features typical of ASP.NETapplications. This mode is mainly targeting situations that require backwardcompatibility or migration of ASMX services.

 

IIS 7 and WAS Hosting Model

On Windows Longhorn Server machines you ll host your WCFservices with Windows Activation Service ( WAS ). WAS is a process activationservice installed with IIS 7 that decouples the activation architecture fromIIS in order to support non-HTTP protocols, such as named pipes, TCP, and MSMQ.Like IIS 6.0, WAS also provides features for idle-time management, healthmonitoring, process recycling, and management tools for configuring applicationpools, among other things.

 

For HTTP requests, the hosting architecture is verysimilar to IIS 6:

  • Requests are first received by the http.syskernel.

  • A new WAS service is responsible for configuringapplication pools, among other things.

  • The WWW Service is responsible for activatingapplication pools and requests queues to process incoming requests. Inaddition, this service has a new HTTP Listener Adapter that forwards requeststo the HttpHandler to process the request (without the need foraspnet_isapi.dll).

  • If ASP.NET compatibility mode is disabled, theHttpModule intercepts the request as discussed in the previous section.

  • If ASP.NET compatibility mode is enabled, theHttpModule allows the request to proceed to the HttpHandler, as discussed.

 

The architecture of IIS 7 and WAS hosting is shown inFigure 5. Of course, ASP.NET is only involved if the service is hosted overHTTP.

 


Figure 5: How IIS 7, WAS, andASP.NET process service requests.

 

There are a many benefits to this architecture, including:

  • Requests no longer go through aspnet_isapi.dllfor processing; instead, requests are directly forwarded to their associatedhandler.

  • Many protocols, including custom (extensible)protocols, can be supported through WAS configuration. This allows you to learna single model for all service deployments, even those behind the firewall.

  • You can reap the rewards of improvements to theconfiguration and health monitoring features for worker processes.

 

Conclusion

At this point you should have a good idea how IIS, ASP.NET,and WCF interact to process requests to services exposed over HTTP. The nicething is that you can allocate application pools for incoming requests toservices, just as you would to ASP.NET application resources. It is in thetransfer to the ASP.NET pipeline when the service model is involved to provideconsistent processing for all services, regardless of their hostingenvironment. If you enable ASP.NET compatibility mode, you ll end up with afamiliar ASMX model as far as the ASP.NET pipeline is concerned, but ideallyyou ll only use this for migration purposes.

 

Additional Resources

WCF Home: http://wcf.netfx3.com

Learning WCF, O Reilly2007: http://www.thatindigogirl.com(get all the book code here!)

Michele s blog: http://www.dasblonde.net

IDesign: http://www.idesign.net

 

Michele LerouxBustamante is Chief Architect at IDesign Inc., Microsoft Regional Directorfor San Diego, Microsoft MVP for Connected Systems, and a BEA TechnicalDirector. At IDesign Michele provides training, mentoring, and high-endarchitecture consulting services, specializing in scalable and secure .NETarchitecture design, globalization, Web services, and interoperability withJava platforms. She is a board member for the International Association ofSoftware Architects (IASA), a frequent conference presenter, conference chairof SD s Web Services and Web Development tracks, and a frequently publishedauthor. Michele recently completed the book, LearningWCF, published by O Reilly (book blog: http://www.thatindigogirl.com). Reachher at http://www.idesign.net or http://www.dasblonde.net.

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like