Question

I am trying to start my debug build WindowsService but am getting the error that it didn't start in a timely fashion.

I've cleaned up the ctor and OnStart but still it won't start so I am thinking maybe the dll it loads needs to be better installed, rather than just in the debug directory of my project directory. Maybe all that bumpf in main() is hanging it out to dry?

Any pointers for debugging the service? I can't run it directly in the VStudio because obviously that doesn't permit entry to its OnCustomCommand(int cmd) member and breaks it there.

Was it helpful?

Solution

If I understand correctly, you want to be able to debug the service before it's sitting in it's idle "Started" state?

You can use Debugger.Break() function for this. For example, we often put the following into our Main function while in a debug build:

#if DEBUG

if (!Debugger.IsAttached)
{
    Debugger.Break();
}

#endif

OTHER TIPS

I would only do as little as possible in the OnStart method, enough init to kick off a thread that does all the work, so you can return from OnStart as quickly as you can. Then in the thread delegate you can add Reddog's code to break into the debugger, possibly with a thread.sleep in there so that it gives you time to attach to the process first.

Another alternative is to add a main method to your service, which can create an instance of the service class and call onstart/onstop, and change the project output to console app, then you get the best of both worlds, it'll install as a service via installutil, and you can f5 run it and debug it in visual studio as a console app.

Details here

If you want to debug the service only as a service, then I would suggest that your OnStart method starts a thread that does the processing and then returns, the thread delegate then can do a Thread.Sleep(some reasonable amount of time) and then Debugger.Break();

The thread sleep gives you a chance to attach the debugger to the process, after you have started the service using service control manager, then it gets to the Debugger.Break() which will force the debugger to break, allowing you to single step. The key thing is to return from OnStart before 30 seconds, and to attach the debugger to the process before you hit your breakpoints.

Also, I think Debugger.Break gives you the option to attach the debugger, so try that in the code (not sure if it plays nicely with services though as they are supposed to have no UI) you might get away with sticking Debugger.Break into your delegate and attaching to the process before 30 secs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top