Hosting WCF Service Within Windows Service

Windows Services are the most useful environment for self-hosting. These are service environments for hosting the WCF service because it is necessary to write the host code individually, identical to that of the console applications. The only difference is in the location where the service is initialized. This type of hosting is used in production conditions for several reasons:
  • Windows services can be configured to boot up with the operating system.
  • Service Control Manager can be used to handle startup, restart, and other operations, so no additional environment is required.
  • The Windows service can be configured to restart when a failure occurs, which does not increase the overall availability. 
What are Windows Services?
Windows services are applications whose lifetime is controlled by the Service Control Manager (SCM) application. SCM runs automatically with the operating system and thus allows local or remote control over Windows services. SCM on Windows operating systems is within the scope of the Services application.
Application Services Appearance on Windows 10

By using the Services application, you can start, pause, stop, or resume Windows service execution. 
Creating a Windows Service 
In order to host the WCF service inside a Windows service, you must first create such a Windows service. The Windows service can be created by using the Visual Studio development environment, using the Windows service window

By creating a project based on the Windows Service Window, the environment automatically carries out several things. The first is to create a service class that inherits the ServiceBase class. This is the basic class for creating a Windows service. The Windows service class environment is named Service1, but using the Rename option can change the name of the service. When the service name changes to ReserveServiceHost, the look of the generated class will be,
  1. public partial class ReverseServiceHost : ServiceBase  
  2. {  
  3.   public ReverseServiceHost()  
  4.   {  
  5.      InitializeComponent();  
  6.   }  
  7.   protected override void OnStart(string[] args)  
  8.   {  
  9.   }  
  10.   protected ovveride void OnStop()  
  11.   {  
  12.   }  
  13. }  
In addition to the fact that the class that represents the Windows service inherits the ServiceBase class, it also has two methods with the names OnStart and OnStop. These methods are called for the occasions of starting and stopping the service - respectively.
The platform was also used to generate code to run the service within the Class Program,
  1. static class Program  
  2. {  
  3.  ///<sumary>  
  4.  ///The main entry point for the application.  
  5.  ///</sumary>  
  6.    static void Main()  
  7.    {  
  8.      ServiceBase[] ServicesToRun;  
  9.      ServicesToRun = new ServiceBase[]  
  10.      {  
  11.      new ReverseServiceHost()  
  12.      };  
  13.      ServiceBase.Run(ServicesToRun);  
  14.    }  
  15. }  
Relocating WCF hosting services within Windows services
In order for a WCF service to be hosted within the Windows service displayed, the appropriate ServiceHost initiation code must be added to the event handler method OnStart, as in the following example,
  1. ServiceHost serviceHost;  
  3. protected override void OnStart(string[] args)  
  4. {  
  5.    serviceHost = new ServiceHost(typeof(MyServices.ReverseService));  
  6.    //Open the Servicehost to start listening for messages.  
  7.    serviceHost.Open();  
  8. }  

In order for the displayed code to be functional, it is necessary to add references to the System.ServiceModel library and the project that contains the service in the Windows service project.

When the Windows service stops running, it is necessary to free the instance of the ServiceHost class by calling the Close method, as in the following example,
  1. protected overide void OnStop()  
  2. {  
  3.    if (serviceHost != null)  
  4.    {  
  5.    serviceHost.Close();  
  6.    }  
  7. }  
To enable the WCF service to be hosted in the displayed way, it is necessary to define the hosting configuration within App.config as follows,
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.  <system.servicemodel>  
  4.    <services>  
  5.     <service name="MyServices.ReverseService">  
  6.     <end point adress="http://localhost:8090/MyServices/ReverseService" binding="basicHttpBinding" 
  7. contract="MyServices.IReverseService" />  
  8.      </service>  
  9.     </services>  
  10.    <standardEndpoints/>  
  11.   <bindings/>  
  12.  </system.serviceModel>  
  13. </configuration>  
In this way, the process of implementing the functionality for hosting the WCF service within the Windows service is completed.