We
will implement windows service hosting for that ,We will use same set of code
used for hosting the WCF service in Console application to this. This is same
as hosting the service in IIS without message activated. There is some
advantage of hosting service in Windows service.
- The service will be hosted, when system starts
- Process life time of the service can be controlled by Service Control Manager for windows service
- All versions of Windows will support hosting WCF service.
Step
1:
Now let start create the WCF service, Open the Visual Studio 2008 and click
New->Project and select Class Library from the template.
Step 2:
Add reference System.ServiceModel to the project. This is the core assembly used for creating the WCF service.
Step 3:
Next we can create the ISimpleCalulator interface as shown below. Add the Service and Operation Contract attribute as shown below.
ISimpleCalculator.cs
Using System;
Using
System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using
System.ServiceModel;
Namespace
WindowsServiceHostedContract
{
[Service Contract]
public interface ISimpleCalculator
{
[Operation Contract]
int Add(int num1, int num2);
[Operation Contract]
int Subtract(int num1, int num2);
[Operation Contract]
int Multiply(int num1,int num2);
[Operation Contract]
double Divide(int num1, int num2);
}
}
Step
4:
Implement the ISimpleCalculator interface as shown below.
SimpleCalulator.cs
Using
System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace WindowsServiceHostedService
{
Class SimpleCalculator: ISimpleCalculator
{
Public int Add (int num1, int num2)
{
Return num1+num2;
}
Public int Subtract (int num1, int
num2)
{
Return num1-num2;
}
Public int multiply (int num1, int
num2)
{
Return num1*num2;
}
Public double Divide (int num1, int
num2)
{
If (num2! = 0)
Return num1 / num2;
Else
Return 0;
}
}
}
Step 6: Open Visual Studio 2008 and Click New->Project and select Windows Service.
Step 7: Add the 'WindowsServiceHostedService.dll'
as reference to the project. This assembly will going to act as service.
Step
8:
OnStart method of the service, we can write the hosting code for WCF. We have
to make sure that we are using only one service host object. On stop method you
need to close the Service Host. Following code show how to host WCF service in
Windows service.
WCFHostedWindowsService.cs
Using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using
System.ServiceProcess;
using System.Text;
using
System.ServiceModel;
using
System.ServiceModel.Description;
Namespace
WCFHostedWindowsService
{
partial class WCFHostedWindowsService :
ServiceBase
{
Service Host m_Host;
public WCFHostedWindowsService()
{
InitializeComponent();
}
protected override void
OnStart(string[] args)
{
if (m_Host != null)
{
m_Host.Close();
}
//Create a URI to serve as the base
address
Uri httpUrl = new
Uri("http://localhost:8090/MyService/SimpleCalculator");
//Create Service Host
m_Host = new Service Host
(typeof(WindowsService Host edService.SimpleCalculator),
httpUrl);
//Add a service endpoint
m_Host.AddServiceEndpoint
(typeof(WindowsService Host edService.ISimpleCalculator),
new WSHttpBinding(), "");
//Enable metadata exchange
ServiceMetadataBehavior smb = new
ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
m_Host.Description.Behaviors.Add(smb);
//Start the Service
m_Host.Open();
}
protected override void OnStop()
{
if (m_Host != null)
{
m_Host.Close();
m_Host = null;
}
}
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new
WCFHostedWindowsService()
};
ServiceBase.Run(ServicesToRun);
}
}
}
Step
9: In
order to install the service we need to have the Installer class for the
Windows service. So add new Installer class to the project, which is inherited
from the Installer class. Please find the below code for mentioning the
Service name, StartUp type etc of the service.
ServiceInstaller.cs
Using System;
Using System.Collections.Generic;
Using System.Text;
Using
System.ServiceProcess;
Using System.Configuration.Install;
Using System.ComponentModel;
Using System.
Configuration;
Namespace
WCFHostedWindowsService
{
[Run Installer (true)]
Public class WinServiceInstaller: Installer
{
Private ServiceProcessInstaller
process;
Private Service Installer service;
Public WinServiceInstaller ()
{
Process = new ServiceProcessInstaller
();
Process. Account = ServiceAccount.NetworkService;
Service = new Service Installer ();
service.ServiceName =
"WCFHostedWindowsService";
service.DisplayName =
"WCFHostedWindowsService";
service.Description = "WCF
Service Hosted";
service.StartType =
ServiceStartMode.Automatic;
Installers.Add (process);
Installers.Add (service);
}
}
}
Step 10: Build the project,
we will get the WCFHostedWindowsService.exe. Next we need to install the
service using Visual Studio Command Prompt. So open the command prompt by
clicking Start->All Programs-> Microsoft Visual Studio 2008-> Visual
Studio Tools-> Visual Studio Command Prompt Using installutil utility
application, you can install the service as shown below.
Step 11: Now service is
Hosted sucessfully and we can create the proxy class for the service and start
using in the client applcaiton.
No comments:
Post a Comment