Debugging windows services

One problem that I have ran into time and time again while working on windows services is the difficulty around needing to debug them.  Sure, you can setup the service within windows and then start it up and try to attach the debugger before your problem area hits.

But there is a better way.

To do this, you just want to update the project main function and create a wrapper class such as the example below…

static void Main(string[] args)
{
  if (Environment.UserInteractive)
  {
    var svc = new MyService();
    svc.StartService(args);
    Thread.CurrentThread.Join();
  }
  else
    ServiceBase.Run(new ServiceBase[] { new AggEnrollmentService() });
}

By doing this, the when you hit F5 from within visual studio, it will behave similar to a console application, but can still be installed and ran just as a regular windows service.  Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s