CoderJony

How to debug a Window Service?

To debug a window service you can make use of below property of Environment class:

Environment.UserInteractive

When you run your code in debug mode, this property returns true because your code is running in user interactive mode but when you run your window service as a windows process or service, then this property returns false.

It is recommended to do not show any popup, alert or dialog when this service runs as windows process.

static void Main(string[] args)
{
    if (Environment.UserInteractive)
    {
        // Your own logic to debug
        MyService myService = new MyService();
        myService.SomeTestMethod();
    }
    else
    {
        // Actual window service code
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new InvoiceProcessor() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}
Buy Me A Coffee