Windows Communication Foundation - SPLessons

WCF Windows Service Hosting

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WCF Windows Service Hosting

WCF Windows Service Hosting

shape Description

WCF Windows Service Hosting option involves registering the WCF Service within the windows service. So that WCF Service lifetime is controlled by the operating system.
  • Like in self-hosting options,these methods should also host the service programmatically.
  • Using this service both windows service and WCF Service can be created and implemented. So inherit the class from service base class of windows service as well as from WCF Service contract interface

Process to host WCF Service in windows services

shape Step-1

First WCF Service is needed. For that take a WCF Service library.Below screen shows how to add WCF service library project.

shape Step-2

Then, there are two default files like Iservice1.cs, Service1.cs. One can change the names for that files like Iwindowshost.cs and windowshost.cs. And write a code in Iwindowshost .cs like below: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceWinHost { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract] public interface Iwindowshost { [OperationContract] string GetCurrentDateTime(); } }[/csharp] Write a code in Windowshost.cs file like below: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceWinHost { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. public class windowshost : Iwindowshost { public string GetCurrentDateTime() { return DateTime.Now.ToString(); } } }[/csharp] The simple WCF Service is ready. Now add windows Service project to this solution. Below screen shows how to add a new project for this solution. Then, the new screen appears like below:

shape Step-3

In the above screen, right click anywhere to get an option called as Add installer. Click on Add installer, a new screen appears with the two options like below: In above screen, change some properties for installation of windows service. Below screen shows how to do that.

shape Step-4

After setting all the properties, add the service reference from the WCF service reference and add "system.servecemodel" assembly reference also. Below screens shows how to add references.

shape Step-5

After adding all the references, write a code for the installation in Service1.cs file like below [csharp]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 WCF_Service; namespace WindowsServiceHost { public partial class Service1 : ServiceBase { ServiceHost host; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { host = new ServiceHost(typeof(WCF_Service.WindowsService)); host.Open(); } protected override void OnStop() { host.Close(); } }[/csharp] Now, build the entire solution to get "Exe" file like "WCFServiceHost.exe". Then open the Visual Studio command prompt with run as an administrator for installing our service into windows like below screen

shape Step-6

In above screen, pass a command like Then press enter to install our service into windows. C:\Users\srinuvarra\Documents\visual studio 2010\Projects\ WCF_Service\WindowsServiceHost\bin\Debug\WindowsServiceHost.exe. This path is coming from where the project is stored in the system. After completing the installation, check whether the service is installed or not. For that, Open the run command window in computer and write in that services.msc. Then it will open new Services window like below. After clicking on start option, windows is attempting to run the service.