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,
- public partial class ReverseServiceHost : ServiceBase
- {
- public ReverseServiceHost()
- {
- InitializeComponent();
- }
- protected override void OnStart(string[] args)
- {
- }
- protected ovveride void OnStop()
- {
- }
- }
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,
- static class Program
- {
-
-
-
- static void Main()
- {
- ServiceBase[] ServicesToRun;
- ServicesToRun = new ServiceBase[]
- {
- new ReverseServiceHost()
- };
- ServiceBase.Run(ServicesToRun);
- }
- }
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,
- ServiceHost serviceHost;
-
- protected override void OnStart(string[] args)
- {
- serviceHost = new ServiceHost(typeof(MyServices.ReverseService));
-
- serviceHost.Open();
- }
Note
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,
- protected overide void OnStop()
- {
- if (serviceHost != null)
- {
- serviceHost.Close();
- }
- }
Note
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,
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <system.servicemodel>
- <services>
- <service name="MyServices.ReverseService">
- <end point adress="http://localhost:8090/MyServices/ReverseService" binding="basicHttpBinding"
- contract="MyServices.IReverseService" />
- </service>
- </services>
- <standardEndpoints/>
- <bindings/>
- </system.serviceModel>
- </configuration>
In this way, the process of implementing the functionality for hosting the WCF service within the Windows service is completed.