Sometimes windows service debugging is really tricky. Usually , during development, developer should implement some test harness application with UI to run and test functionality being developed. But when it comes to debug real windows service which hosts the objects which do the real job - it's a headache.
Amazingly, in NET platform there is a simple and straightforward way to debug a windows service.
Consider the following code sample:
using System.ServiceProcess;
namespace MyService
{
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch(); //This will launch a debugger
//Do the real job MyWorker.Start();
}
}
}
After you installed and launched the service , you will be prompted to use Visual Studio to attach to the process. Click "Attach" button and Visual Studio will pop up and here you go.