Monthly Archives: March 2012

Windows Service testing

This is a handy way to test your windows service; something I have been doing for awhile. I was looking at it recently, and thought it might be handy for someone else:

public static class Program
{
///
/// The main entry point for the application. /// 

public static void Main()
{
#if (!DEBUG)
  ServiceBase[] ServicesToRun;
  ServicesToRun = new ServiceBase[] { new MyWinService() };
  ServiceBase.Run(ServicesToRun);
#else
  //Debug code: this allows the process to runĀ 
  // as a non-service. It will kick off the
  // service start point, and then run theĀ 
  // sleep loop below.
  MyWinService service = new MyWinService();
  service.Start();
  // Break execution and set done to true to run Stop()
  bool done = false;
  while (!done)
   Thread.Sleep(10000);
  service.Stop();
#endif
}

By using this approach, you can compile a release version of the service that runs like a service, and while debugging, you can run the loop in the debug section that allows you to break and set done and fully test your startup and shutdown code.

HTH,

Robb

Advertisements