Hosting WCF Service: Part 2

Introduction

For a WCF service to be available for the clients to consume, we need to host the WCF service. I highly recommend reading the previous part:

  1. Hosting WCF Service: Part 1

Windows Service

It is similar to any other program or application running on a Windows machine. The following are the differences between a Windows Service and a regular application.

  1. Windows Services run in the background.
  2. They can be configured to start automatically.
  3. They don't have a user interface.

How to view all the services installed on a Windows Service:

  1. Open the Run window (start + R).

  2. Type services.msc and Enter.

  3. The services window should display all the services installed on your computer like the following:

    services

    services installed

In general we create a Windows Service to run code in the background all the time, without any sort of user interaction. A developer can use a Windows Service to host a WCF service. We can configure that to start automatically when your computer starts. This makes our service available for clients to consume. the following is the procedure for creating a service as Windows Service. To create a WCF service hosted in a Windows Service read the first article about how to create a WCF service and host in a console application Hosting WCF Service: Part 1.

Create a Windows Service to host a WCF Service:

  1. Open Visual Studio.

  2. Select File –> New Project.

  3. Select Windows from left side then select Windows Service.

  4. Provide the name as you want such as "WindowsServiceHost".

    Windows Service

  5. Add the two references for System.ServiceModel and the other is the WCF service reference. Here I created a calculation service.

  6. Add the application config file.

  7. Right-click on the project then select Add -> New Item.

  8. Select App.config file then click OK.

  9. In that configuration file we provide the WCF service connection like endpoints. Just copy the previously created app.config file of the console application and paste it in.

  10. Change the Service1 file name to CalWindowsService.

  11. Double-click on that file. It will look like the following. Right-click on that file then click View Code.

    View Code

    When you click view code it will have the following code:
    1. public partial class CalWindowsService : ServiceBase  
    2. {  
    3.     public CalWindowsService()  
    4.     {  
    5.         InitializeComponent();  
    6.     }  
    7.   
    8.     protected override void OnStart(string[] args)  
    9.     {  
    10.     }  
    11.   
    12.     protected override void OnStop()  
    13.     {  
    14.     }  
    15. }  
    Change the code like the following to open the communication channel to consume the service for the client.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel;  
    4. using System.Data;  
    5. using System.Diagnostics;  
    6. using System.Linq;  
    7. using System.ServiceProcess;  
    8. using System.Text;  
    9. using System.ServiceModel;  
    10.   
    11. namespace WindowsServiceHost  
    12. {  
    13.     public partial class CalWindowsService : ServiceBase  
    14.     {  
    15.         ServiceHost host;  
    16.         public CalWindowsService()  
    17.         {  
    18.             InitializeComponent();  
    19.         }  
    20.   
    21.         protected override void OnStart(string[] args)  
    22.         {  
    23.             //for to start the service  
    24.             host = new ServiceHost(typeof(CalService.CalculationService));  
    25.             host.Open();  
    26.         }  
    27.   
    28.         protected override void OnStop()  
    29.         {  
    30.             //for to stop the service  
    31.             host.Close();  
    32.         }  
    33.     }  
    34. }  
  12. Open the CalWindowsService file then right-click on something then select Add Installer like the following:

    Add Installer

    When you click on Add Installer it will add the ProjectInstaller.cs file.

    ProjectInstaller

    You can see in the preceding image that there is a newly added file and on the left side there are two instances, one is ServiceProcessInstaller1 and the second is ServiceInstaller1.

  13. Select ServiceInstaller1. Right-click on something then select Properties like the following:

    Properties

    The properties window will be opened in the right side, select the Service Name property. Change the name Service1 to CalWindowsService. Select Start Type to you want. In other words, if you select Automatic then it will start automatically when windows starts like other applications. Or select Manual to start it manually.

    Manual

  14. Now select servicePreocessInstaller1 and right-click then select Properties like the following:

    Select Properties

    Now here you need to select the Account properties. To allow the user to access this service select LocalSystem.

  15. Save the project and build it.

    Save the project

Install the Windows Service using the installutil.exe

To install the new Windows Service use the following procedure.

  1. Open a Visual Studio Command Prompt in Administrator mode.

    Command Prompt

  2. Now navigate to the C:\ to install the service.

  3. Use the following syntax to install and uninstall the Windows Service.

    Install:

    Installutil –i windows_service_path/projectname.exe + Enter

    Uninstall:

    Installutil –u windows_service_path/projectname.exe + Enter

  4. Install our Windows Service as in the following; for this select the project path of the .exe file.

    Windows Service host

    Install our Windows Service

    Now you open the installed service Start + R and type services.msc. It will open like the following:

    Select Start

    But it is not started yet so just right-click then select Start/Stop Service like the following:

    Start

    When the service is starting it will show like this:

    service control

    CalWindowsService

    The preceding image shows the CalWindowsService is running.

  5. Uninstall a Windows Service like the following:

    Uninstall Windows Service

  6. It's time to test the application. Run the console application we hosted the service in as follows:
    1. class Program  
    2. {  
    3.     static void Main(string[] args)  
    4.     {  
    5.         CalService.CalculationServiceClient cal = new CalService.CalculationServiceClient();  
    6.         var add = cal.ADD(20,20);  
    7.         var sub = cal.SUB(30, 20);  
    8.         var mult = cal.MULT(10, 2);  
    9.         var div = cal.DIV(20, 2);  
    10.         Console.WriteLine("!! Article By Jeetendra !!");  
    11.         Console.WriteLine("Addition is : " + add);  
    12.         Console.WriteLine("Substraction is : " + sub);  
    13.         Console.WriteLine("Multiplication is : " + mult);  
    14.         Console.WriteLine("Division is : " + div);  
    15.         Console.Read();  
    16.     }  
    17. }   
    Output

    Output

Note: For detailed code please download the Zip file attached above.

Summary

I hope you understand the concepts of Hosting a WCF Service as a Windows Service and that this is useful for all readers. If you have any suggestion regarding this article then please contact me.


Similar Articles