NuGet Package ID: CodeBits.WindowsServiceRunner

When creating a Windows Service, you'd typically want it to run as a console application during development to help with debugging and to avoid the unregistration/registration steps after each build.

The WindowsServiceRunner class helps with this by executing the same service code as either a console application or as an actual Windows service, depending on the project type:
  • If the project type is Console Application, the code is executed as a command-line application.
  • If the project type is Windows Application, the code is compiled as a Windows service and can be installed accordingly.

Note: Version 1.3 includes changes that break compatibility with earlier versions. This page documents the the features of 1.3 and above.

In your Windows service project's Program.cs file, change your Main method to the following (replacing MyService with the name of your service class):
var runner = new WindowsServiceRunner();

Alternatively, you could run multiple service classes:
var runner = new WindowsServiceRunner();

You could also use the constructor to specify the service types, instead of using the Register method:
var runner = new WindowsServiceRunner(new[] { typeof(MyService1), typeof(MyService2) });

However, the Register method is useful if you want to call a non-default constructor on the service class:
var runner = new WindowsServiceRunner();
runner.Register<MyService>("First Param", 2, true);

If all you want to do is start one or more services using their default constructors and not have any other customization like assigning event handlers, you could use the static Run method on the class:
WindowsServiceRunner.Run(typeof(MyService1), typeof(MyService2));

Note: Do not use the Console.Write methods to output content to the console window. Instead, use the Debug.Write and Trace.Write methods. WindowsServiceRunner will automatically register a ConsoleTraceListener whenever the service is run in console mode.

Last edited Nov 4, 2012 at 8:47 PM by jeevanjj, version 7


No comments yet.