Hosting a WCF Service Under a Windows Service

Introduction
 
In this article, we will see how to host our WCF service in a Windows environment under a Widows Service. You can read about how to create and consume a WCF service here. When we run our WCF service then it is hosted by WcfTestClient automatically and for using the WCF service we need to open and run our service application manually which is not good. To host our WCF service we can use various ways like IIS hosting and Windows Service hosting. In this article, we will see how to host our WCF service under a Windows service.
 
If we host our WCF service under a Windows service then it will start when our Windows (OS) starts and stop when our OS stops. To do that we need to create one Windows service application. So let's create one Windows service application using the following steps.
 
Step 1
 
Open your Visual Studio if you are using Windows Vista or Windows 7, then open Visual Studio in Administrator mode and create a new project of type Windows Service like in the following diagram.
 
service.gif 
 
Step 2
 
Add a reference to your WCF service library from Project Add Reference Browse Select your WCF service .dll as well add a reference to System.ServiceModel namespace from the .NET tab.
 
Step 3
 
When we created a new Windows service project, by default we got the Service1.cs file. Click on the "switch to code view" link on Service1.cs file which will open the code view which has methods i.e. OnStart and OnStop methods where we can write the code to perform our own actions when the service starts and when the service stops. We will start our service and stop our service in this method so make your Service1.cs code file like below.
  1. public partial class Service1 : ServiceBase  
  2. {  
  3.     ServiceHost obj;  
  4.     public Service1()  
  5.     {  
  6.         InitializeComponent();  
  7.     }   
  8.     protected override void OnStart(string[] args)  
  9.     {  
  10.         obj = new ServiceHost(typeof(AuthorService.Service1));  
  11.         obj.Open();  
  12.     }   
  13.     protected override void OnStop()  
  14.     {  
  15.         obj.Close();  
  16.     }  
  17. }  
In the above code we have written the code for starting our service and stoping our service. In the above lines you can see we are using ServiceHost which is the class present in System.ServiceModel namespace and this class will give the environment to host our WCF service.
 
Still now we have created our application and Start and Stop methods but we also have to configure it like ABC (Address, Binding and Contract). For configuration add one Application configuration file in your Windows Service application which empty. In the <Configuration> tag we have to write our WCF service configuration i.e. ABC instead of writing it just copy the <System.ServiceModel> tag from your WCF service App.Config file and paste it into the app.config file of Windows service application.
 
Step 4
 
Now our Windows service is ready, but we cannot install it directly so we need a service installer and a process installer; for adding them, go to design view of service1.cs in Windows service application right-click and select Add Installer which will add a ServiceInstaller and ServiceProcessInstaller. Select ServiceInstaller and set the property DisplayName to what you want to display the service in the service tray (Control Panel Adm. Tools Services) and StartType to Automatic or Manual. If you select Automatic then your service will start automatically and if you set manual then we have to start our service manually from Control Panel Adm.Tools Services. Next Select ServiceProcessInstaller and set the property Account to LocalSystem and build the application which will create one exe file which we need to register with OS.
 
Now we are ready to install our service. Here installing the service means registering our service with Windows. To register our service read more below.
 
Step 5
 
To register your service, open your Visual Studio command prompt in Administrator mode and switch to your project directory i.e. Windows Service Project and Register the generated exe file using installutil command followed by exe name which will register our service with Windows. Now you can see your service by opening Control Panel Adm. Tools Services Display Name which you set for ServiceInstaller.
 
Conclusion
 
Using simple steps we can host our WCF service with a Windows service.


Similar Articles