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', binding Information='808:*']
That command adds the net.tcp site binding to the default web
site by modifying the application Host.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 Service Contract attribute to the interface and
add Operation Contract 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;
[Service Contract]
Public interface IMathService
{
[Operation Contract]
int Add(int num1, int num2);
[Operation Contract]
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 Math Service:
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
<%@ Service Host
Language="C#" Debug="true" Service="Math Service"
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="Math Service"
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 -->
<service Metadata
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 -->
<service Debug
includeExceptionDetailInFaults="false"/>
</behavior>
</service Behaviors></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/WASHostedService"
/enabledProtocols:http,net.tcp
Step
6: Now the service is ready to
use. Next we can create the proxy class using service utility 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.
No comments:
Post a Comment