Hosting a service in Windows Service

Windows Services are widely used on Windows operating systems for hosting applications that will perform some long-run or scheduled tasks in the background. Applications hosted via Windows Service can be running under a specific user account and can choose the startup mode (manually or automatically). As a popular service-application-hosting scenario, it is also quite common to deploy a WCF service as a Windows Service.

How to do it...

In this recipe, we will use a typical .NET-based Windows Service to demonstrate how to host a WCF service in a Windows Service application. Let's go through the detailed steps:

  1. Create a Windows Service project.

    The first step is to create a new Windows Service project through the Visual Studio IDE. When creating the project, we simply choose the Windows Service project type. The following screenshot shows how we can select the Windows Service project type in the Visual Studio New Project wizard.

    How to do it...
  2. Add a new WCF service item.

    As a WCF service hosting application, we certainly need to have a WCF service defined here. The steps for creating a WCF service are the same as what we've discussed in the Hosting a service in console application recipe.

  3. Add service hosting code into the service startup and shutdown event.

    As for the service-hosting code in the Windows Service, we need to put it in the correct place, since the .NET-based Windows Service type doesn't directly expose the Main function. The following code shows how the WCF service startup and shutdown code is defined:

    public partial class Service1 : ServiceBase
    {
       ServiceHost _svcHost = null;
    
       protected override void OnStart(string[] args)
       {
          // Start the service host here
          _svcHost = new ServiceHost(typeof(TestService));
          _svcHost.Open();
       }
    
       protected override void OnStop()
       {
          // Close the service host
          _svcHost.Close();
       }
    }
  4. Add an installer for the Windows Service.

    Now the Windows Service class and WCF service types have been defined. However, we still need to add another component—the installer class for deploying the Windows Service into the Windows Service collection on the target operating system. In the Visual Studio IDE, we can simply add an installer type for the Windows Service by the context menu on the component designer. The following screenshot shows the context menu item for creating the installer class for the Windows Service.

    How to do it...

    The IDE will help create two helper classes—one is of ServiceProcessInstaller type and another of ServiceInstaller type. We can specify many deployment parameters for the Windows Service in the Property panel of the two classes. The following screenshot shows the properties of the sample serviceProcessInstaller1 class.

    How to do it...

    The next screenshot shows the properties of the sample serviceInstaller1 class.

    How to do it...

    As with the screenshots displayed, Visual Studio will use standard Properties windows for displaying and configuring the individual properties of the Windows Service classes.

  5. Install the Windows Service through Installutil.exe.

    The last step is to install the Windows Service we have created (after building the project) into the operating system. This can be done by using the Installutil.exe tool provided by the .NET framework. You can directly execute the Installutil.exe command within the Visual Studio command-line prompt window or you can choose to launch the tool through its absolute path in the .NET framework folder such as C:\Windows\Microsoft.NET\Framework\v4.0.30319.

    Note

    The following statements show the complete commands for installing and uninstalling a .NET-based Windows Service application via the Installutil.exe tool.

    Install the Windows Service:

     InstallUtil.exe WCFNTService.exe

    Uninstall the Windows Service:

     Install Util.exe /u WCFNTService.exe

    The WCFNTService.exe mentioned earlier is the output assembly name of the sample Windows Service project.

How it works...

The OnStart event is fired when the Windows Service is starting, while the OnStop event is fired when the Windows Service is shutting down. Therefore, they are the best places for us to put the WCF service-hosting code.

Sometimes, we may need to access some remote or protected resource in our Windows Service host program. In such cases, it is important to specify a proper service account, either at development time or in the Windows Service Configuration Manager. The following screenshot shows the service list, which contains the installed sample Windows Service in Windows Service Configuration Manager.

How it works...

See also

  • Hosting a service in a console application
  • Complete source code for this recipe can be found in the \Chapter 3\recipe2\ folder