Library Kata "Windows Service"

Implement a library with which a Windows service can be realized in a simple way.

As a user of a Windows service, I would like to be able to install it with the following command on the command line:

C:\> myservice /install

This means that the service is known in the system and is visible under System settings/Services. I would then like to start it with the operating system command

C:\> net start myservice

I would also like to be able to stop him with

C:\> net stop myservice

The service should be able to be uninstalled again with the following command:

C:\> myservice /deinstall

The developer of a Windows service must implement the following interface in order to be able to use the library:

public interface IService
{
    string Name { get; }

    string DisplayName { get; }

    string Description { get; }

    void OnStart();

    void OnStop();
}

A class that implements this interface can then be executed as a Windows service with the help of the library. This is to be implemented using the EasyService be possible as follows:

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

In this example, the class MyService the interface shown above IService. The instance and the command line arguments are sent to the Run method. Run interprets the arguments to install or uninstall the service as required.

Variation #1

In order not to be forced to use the interface IService the service should also be able to be started in the following way:

public static void Main(string[] args) {
   var myService = new MyService();
   EasyService.Run(
      "myservice",
      "MyService",
      "A pretty 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 defined by lambda expressions of type Action handed over. MyService the interface IService cannot be implemented.

Variation #2

I want to be able to run the service without installation by entering the following command on the command line:

C:\> myservice /run

This makes it easier to debug the service.

en_USEnglish