Programming4us
         
 
 
Windows

SOA with .NET and Windows Azure : Service Hosting with WCF (part 3) - Managed Windows Services

12/15/2010 11:30:42 AM

Managed Windows Services

Enterprise server applications require high availability, reliability, and a management view for on-going maintenance. Windows services provides these features.

Using Windows services to host a service requires hosting the ServiceHost instance from the WCF API in System.ServiceProcess. A Windows service is created in .NET by inheriting from the ServiceBase class, which lives in the System.ServiceProcess assembly. When a Windows service starts up, it calls the OnStart method and the ServiceHost instance is executed. Once the Windows service stops, the ServiceHost is closed, thereby tying the lifespan of the WCF service to that of the Windows service:

Example 5.
using System.ServiceModel;
using System.ServiceProcess;
public class AccountWindowsService : ServiceBase
{
ServiceHost serviceHost;
public override void OnStart( string[] args )
{
this.serviceHost = new ServiceHost( typeof( T ) );
this.serviceHost.AddEndpoint( ... );
this.serviceHost.Open();
}
public override void OnStop()
{
this.serviceHost.Close();
}
}

Hosting in Windows services is useful when the lifetime of the service needs to be controlled by the operating system. It is also beneficial when the service has to run under the identity of a restricted account in the background and can use any transport included in WCF.

IIS Process Boundary

In IIS, an application is the application domain and an application pool is the process boundary. IIS includes health monitoring and recycling, allowing it to monitor both the application domain and the application pool and to recycle them if it determines idle behavior (indicating that IIS has choked and is not able to service message requests).

IIS further includes on-demand service activation via HTTP, which means that an IIS application pool is only activated by the IIS infrastructure when it receives an HTTP message. IIS also manages the lifecycle of the host.

Some benefits of hosting a service in IIS include:

  • scalability and several clustering features

  • process recycling, health monitoring, idle shutdown, message based activation

  • security

Hosting a service in IIS 5.0 and 6.0 limits the transport to HTTP and HTTPS. IIS 7.0 includes Windows Activation Services and can host services with other transport protocols.

A service that needs to be hosted in IIS is typically represented by a .svc file, and with ASP.NET it is separated from content using code-behind files. The .svc file uses the ServiceHost directive to identify and link to the code-behind file containing the service class, as shown here:

Example 6.
<%
@ServiceHost language="cs"
Class="GreetingService"
CodeBehind="~/App_Code/GreetingService.cs"
%>

The .svc file can be hosted in an IIS virtual directory, but in order to make a service public, it must be configured in the Web.Config file:

Example 7.
<system.serviceModel>
<services>
<service type="GreetingService">
<endpoint
address=""
binding="basicHttpBinding"
contract="GreetingService" />
</service>
</services>
</system.serviceModel>

Typically, the address in the Web.Config file is set to an empty string, as this address is relative to the address of the .svc file in IIS. To host this .svc file in IIS, the .svc extension must be mapped to the ASP.NET process (aspnet_isapi.dll), which can be set in the IIS Application Configuration Window.

Once a service is hosted in IIS, it can be accessed using a Web browser. In this case, the URL to access the service is:

http://localhost/GreetingService.svc

Windows Activation Services (WAS)

Windows Activation Service (WAS) is a system service introduced with Windows Vista. It is the activation part of IIS 7.0 and can also be installed and configured separately.

IIS 5.0 and IIS 6.0 are appropriate hosting platforms as long as the only transport used is HTTP. WAS is built upon the IIS 6.0 activation model by generalizing the IIS 6.0 process model and extending it to other transports, such as TCP and MSMQ (Figure 1).

Figure 1. The Windows Activation Services (WAS) architecture is built upon the IIS 6.0 activation model.


WAS architecture introduces listener adapters—infrastructure components that bridge particular network protocols to the WAS interface. The listener adapter communicates messages to WAS and requests that it activate worker processes. WAS includes listener adapters for all activation scenarios in WCF, including http, net.tcp, net.pipe, and net.msmq.

Since WAS is built on IIS 6, it provides all the features included in IIS, such as process recycling, application pooling and isolation, identity management, health activation monitoring, message based activation, and so on. Similar to hosting a service in IIS, hosting a service in WAS requires a .svc file which is a pointer to a code-behind file containing the service class.

Hosting REST Services

WCF provides the WebServiceHost for REST-based services, which is a REST-aware equivalent of the ServiceHost class. WebServiceHost ensures that the transport is HTTP and wires up the service endpoints to the REST dispatcher. It also works with WebServiceHostFactory and the WCF configuration-less model.

Each .svc file contains two WCF-specific attributes:

  • Service – indicates which type implements the service behind the endpoint

  • Factory – determines which ServiceHostFactory to use as a host for the service implementation

When the Factory attribute is absent, the supporting WCF infrastructure will set the value to System.ServiceModel.Activation.ServiceHostFactory. This particular factory depends on the Web.Config file to figure out how to host the service, expose endpoints, and attach any behaviors.

Because a REST service has a well-defined set of behaviors, instead of explicitly stating what the binding and behavior set looks like, you can set the Factory attribute to System.ServiceModel.Activation.WebServiceHostFactory and skip any configuration altogether. In fact, the following CatalogService (found in CatalogService.svc and CatalogService.svc.cs) does just that:

Example 8.
<%@
ServiceHost Language="C#" Debug="true"
Service="StandardMold.Catalog.CatalogService"
Factory="System.ServiceModel.
Activation.WebServiceHostFactory"
%>

Other -----------------
- SOA with .NET and Windows Azure : Service Implementation with WCF (part 2)
- SOA with .NET and Windows Azure : Service Implementation with WCF (part 1)
- Windows 7 : Thwarting Spam with Windows Live Mail’s Junk Filter (part 2) - Blocking Countries and Languages
- Windows 7 : Thwarting Spam with Windows Live Mail’s Junk Filter (part 1)
- Windows 7 : Configuring Windows Defender to Scan Email
- Windows 7 : Protecting Yourself Against Email Viruses
- Windows 7 : Understand Internet Explorer’s Advanced Security Options
- SOA with .NET and Windows Azure : WCF Services - Overview
- SOA with .NET and Windows Azure : Web Services (ASMX and WSE)
- Windows 7 : Enhancing Your Browsing Security (part 6) - Managing Add-Ons
- Windows 7 : Enhancing Your Browsing Security (part 5) - Encoding Addresses to Prevent IDN Spoofing
- Windows 7 : Enhancing Your Browsing Security (part 4) - Thwarting Phishers with the SmartScreen Filter
- Windows 7 : Enhancing Your Browsing Security (part 3) - Changing a Zone’s Security Level
- Windows 7 : Enhancing Your Browsing Security (part 2) - Adding and Removing Zone Sites
- Windows 7 : Enhancing Your Browsing Security (part 1) - Blocking Pop-Up Windows
- Windows 7 : Configuring Internet Explorer Security - Enhancing Your Browsing Privacy (part 4) - InPrivate Browsing and Filtering
- Windows 7 : Configuring Internet Explorer Security - Enhancing Your Browsing Privacy (part 3) - Enhancing Online Privacy by Managing Cookies
- Windows 7 : Configuring Internet Explorer Security - Enhancing Your Browsing Privacy (part 2) - Clearing the Address Bar List
- Windows 7 : Configuring Internet Explorer Security - Enhancing Your Browsing Privacy (part 1)
- Windows 7 : Managing Windows Firewall (part 2)
 
 
Most View
- Windows Server 2008 : Configuring FTP (part 10) - Configuring FTP SSL Settings
- SharePoint Server 2010 Business Intelligence Platform (part 5) - PowerPivot
- Microsoft Dynamic AX 2009 : The Batch Framework (part 1) - Batch Processing in Dynamics AX, Common Uses of Batch Processing
- Exchange Server 2003 : Virtual Servers - Configuring Authentication
- Exchange Server 2010 : Manage Access for Mobile Devices (part 2) - Manage Mobile Device Features and Settings
- Active Directory Domain Services 2008 : Transfer the PDC Emulator Role
- iPad SDK : PDF Generation
- Coding JavaScript for Mobile Browsers (part 4)
- Exchange Server 2010 : Manage Outlook Client Access (part 1) - Configure Outlook Anywhere
- Windows Server 2008 : Configure a Load-Balanced Farm with TS Session Broker
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS