Windows Activation Service
Windows Activation service is a system service available with Windows vista and windows server 2008. It is available with IIS 7.0 and it is more powerful compared to IIS 6.0 because it supports Http, TCP and named pipes were IIS 6.0 supports only Http. It can be installed and configured separately.
Hosting WCF in Activation service takes many advantages such as process recycling, isolation, idle time management and common configuration system. WAS hosted service can be created using following steps
- Enable WCF for non-http protocols
- Create WAS hosted service
- Enable different binding to the hosted service
Enable WCF for non-http protocols
Before Start creating the service we need to configure the system to support WAS. Following are the step to configure WAS.
- Click Start -> Control Panel -> programs and Features and click 'Turn Windows Components On or Off' in left pane.
- Expand 'Microsoft .Net Framework 3.0' and enable "Windows Communication Foundation HTTP Activation" and "Windows Communication Foundation Non- HTTP Activation".
- Next we need to add Binding to the Default Web site. As an example, we will bind the default web site to the TCP protocol. Go to the Start menu -> Programs ->Accessories. Right click on the "Command Prompt" item, and select "Run as administrator" from the context menu.
- Execute the following command
-
C:\Windows\system32\inetsrv> appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',
bindingInformation='808:*']
- That command adds the net.tcp site binding to the default web site by modifying the applicationHost.config file located in the "C:\Windows\system32\inetsrv\config" directory. Similarly we can add different protocols to the Default Web site.
Create WAS hosted service
Step 1: Next we are going to create the service, Open the Visual Studio 2008 and click New->WebSite and select WCF Service from the template and Location as HTTP as shown below.
Step 2: Create the Contract by creating interface IMathService and add ServiceContract attribute to the interface and add OperationContract attribute to the method declaration. IMathService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
[ServiceContract]
public interface IMathService
{
[OperationContract]
int Add(int num1, int num2);
[OperationContract]
int Subtract(int num1, int num2);
}
Step 3: Implementation of the IMathService interface is shown below. MathService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
public class MathService : IMathService
{
public int Add(int num1, int num2)
{
return num1 + num2;
}
public int Subtract(int num1, int num2)
{
return num1 - num2;
}
}
Step 4: Service file is shown below. MathService.svc
<%@ ServiceHost Language="C#" Debug="true" Service="MathService"
CodeBehind="~/App_Code/MathService.cs" %>
Step 5: In web.Config file, create end point with 'netTcpBinding' binding and service metadata will be published using Metadata Exchange point. So create the Metada Exchange end point with address as 'mex' and binding as 'mexTcpBinding'. Without publishing the service Metadata we cannot create the proxy using net.tcp address (e.g svcutil.exe net.tcp://localhost/WASHostedService/MathService.svc ) Web.Config
<system.serviceModel>
<services>
<service name="MathService" behaviorConfiguration="ServiceBehavior">
<!-- Service Endpoints -->
<endpoint binding="netTcpBinding"
contract="IMathService" >
</endpoint>
<endpoint address="mex"
binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below
to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in
faults for debugging purposes, set the value below to true.
Set to false before deployment to avoid disclosing
exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors></behaviors>
</system.serviceModel>
Enable different binding to the hosted service
- Go to the Start menu -> Programs ->Accessories. Right click on the "Command Prompt" item, and select "Run as administrator" from the context menu.
- Execute the following command C:\Windows\system32\inetsrv>appcmd set app "Default Web Site/WASHostedServcie" /enabledProtocols:http,net.tcp
Output will be shown below.
Step 6: Now the service is ready to use. Next we can create the proxy class using service uttility and add the proxy class to the client application. Creat the proxy class using Visual Studio Command prompt and execute the command svcutil.exe net.tcp://localhost/WASHostedService/MathService.svc
Proxy and configuration file are generated in the corresponding location.
Step 6: Create the client application as shown below and add the reference 'System.ServiceModel', this is the core dll for WCF.
Step 8: Add the proxy class and configuration file to the client application. Create the object for the MathServiceClient and call the method. Program.cs
class Program
{
static void Main(string[] args)
{
MathServiceClient client = new MathServiceClient();
Console.WriteLine("Sum of two number 5,6");
Console.WriteLine(client.Add(5,6));
Console.ReadLine();
}
}
The output will be shown as below.
So this tutorial clearly explains about the hosting the WCF in Windows Activation Service. So next we can see how to host the service using Windows Service
Tips!
- Always create the service with Interface->Implementation format, mention the contract in Interface.
- Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
- Change the instance mode to per call as default.
- Always catch exception using try/catch block and throw exception using FaultException < T >.
- Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.
|