Step 1) Create Windows Service Application As shown below
Step 2) Add references
1) System.ServiceModel
2) System.ServiceProcess
Step 3) Add new class WsHost.cs (for wcf interface + implementation)
FileName: wsHost.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace WindowsService1
{
[ServiceContract]
interface IOrder1
{
[OperationContract]
int GetOrderCount();
[OperationContract()]
string getCustomername();
}
public class wsHost:IOrder1
{
public int GetOrderCount()
{
return 100;
}
public string getCustomername()
{
return "Customer Name is jhon peter paul";
}
}
}
c:\Users\Administrator\Documents\Visual Studio 2010\Projects\WindowsService1\Win
dowsService1\bin\Debug>installutil WindowsService1.exe
Step 2) Add references
1) System.ServiceModel
2) System.ServiceProcess
Step 3) Add new class WsHost.cs (for wcf interface + implementation)
FileName: wsHost.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace WindowsService1
{
[ServiceContract]
interface IOrder1
{
[OperationContract]
int GetOrderCount();
[OperationContract()]
string getCustomername();
}
public class wsHost:IOrder1
{
public int GetOrderCount()
{
return 100;
}
public string getCustomername()
{
return "Customer Name is jhon peter paul";
}
}
}
Step 4) Implement Service host in derived ServiceBase class
FileName:Service1.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;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
ServiceHost host = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (host != null) host.Close();
host = new ServiceHost(typeof(wsHost),
new Uri("http://localhost:111/wsHost"));
host.Open();
}
protected override void OnStop()
{
if (host != null) host.Close();
host = null;
}
}
}
Step 5) Implement Installer class
After adding installer.cs
replace with the following content
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
namespace WindowsService1
{
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
ServiceProcessInstaller process;
ServiceInstaller service;
public Installer1()
{
InitializeComponent();
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "WCF host in Windows Service";
service.Description = "Hosting wcf service in Windows Service";
service.StartType = ServiceStartMode.Automatic;
Installers.Add(process);
Installers.Add(service);
}
}
}
Step 6) Add application config & change copy to output directory option as shown below.
replace app.config content
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyserBehaviour">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WindowsService1.wsHost" behaviorConfiguration="MyserBehaviour">
<endpoint contract="WindowsService1.IOrder1" binding="basicHttpBinding"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
Step 7) Open VS2010 Command Prompt
got application folder bin directory in this case "c:\users\administrator\documents\visual studio 2010\Projects\WindowsService1\WindowsService1\bin\debug"
c:\Users\Administrator\Documents\Visual Studio 2010\Projects\WindowsService1\Win
dowsService1\bin\Debug>installutil WindowsService1.exe
Installutil.exe is used for installing Windows Services
Step 8) Goto Service Manager as shown below
It will display all services in the system, select "Wcf Host in Windows Service" and start the service
Step 9) Open Browser and type "http://localhost:111/wshost"
OUTPUT
TEST ALL METHODS WITH WCFTESTCLIENT
Open VS2010 command prompt type
WcfTestClient
i.e c:> wcftestclient.exe
and then give above url/add service http://localhost:111/wshost
No comments:
Post a Comment