A better windows service helper

I’ve updated my windows service helper so that it also can install your service for you (through the command line).

Easier debugging

The first thing that you can do is to run your windows service as a console application. That makes debugging a lot easier since you just can start the application (F5) without having to attach to a process.

To be able to do that, you have to add -console as an debugging argument:

Shows the debug settings under project properties

How to configure Visual Studio

Next you’ll have to add the following to your Program.cs:

internal static class Program
{
    private static void Main()
    {
        // Enable running as a console app
        if (WindowsServiceHelper.RunAsConsoleIfRequested<Service1>())
            return;

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
            {
                new Service1()
            };
        ServiceBase.Run(ServicesToRun);
    }
}

Now you can just run the application. A console window will be created automatically.

Install/Uninstall using command line arguments

You can also enable installing/uninstalling your application by using command arguments.

Just add the following to Program.cs:

internal static class Program
{
    private static void Main()
    {
        // Enable install/uninstall
        if (WindowsServiceHelper.ManageServiceIfRequested(Environment.GetCommandLineArgs()))
            return;

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
            {
                new Service1()
            };
        ServiceBase.Run(ServicesToRun);
    }
}

To install:

yourApp.exe -install

To uninstall:

yourApp.exe -uninstall

The code


  • Zoubidou

    What’s wrong with TopShelf?