Windows Service

Library Kata „Windows Service“

Implement a library that makes it simple to implement Windows services.

As a user I want to install a Windows service with the following command:

C:\> myservice /install

The command installs the service so that it is shown in Windows under Control Panel / Services. The following Windows command should start the service after it was installed:

C:\> net start myservice

It can be stopped with the following Windows command:

C:\> net stop myservice

The service should be removed from the system with the following command:

C:\> myservice /deinstall

The library should be implemented so that the developer has to implement the following interface if he wants to implement a Windows service:

public interface IService
{
    string Name { get; }

    string DisplayName { get; }

    string Description { get; }

    void OnStart();

    void OnStop();
}

A class that implements this interface can be run as a Windows service by using the library. The library has to contain a class EasyService that is to be used like the following:

public static void Main(string[] args) {
   var myService = new MyService();
   EasyService.Run(myService, args);
}

In this example the class MyService implements the interface IService. An instance of the class as well as the command line arguments is given to the static Run method. The method interprets the command line arguments and installs or uninstalls the service.

Variation #1

To make it possible to use the library without implementing the IService interface the Run method should have an overload where the necessary information is give as parameters:

public static void Main(string[] args) {
   var myService = new MyService();
   EasyService.Run(
      "myservice",
      "MyService",
      "A realy useless service.",
      () => myService.OnStart(),
      () => myService.OnStop(),
      args);
}

The first three string parameters correspond to the properties Name, DisplayName and Description of the interface. OnStart and OnStop are lambda expressions of type Action. The class MyService now does not have to implement the IService interface.

Variation #2

It should be possible to run the service without installing it by the following command line:

C:\> myservice /run

That would make debugging the service more simple.