Debugging a Windows Service: Windows 2000 Issue
At work we had to create a windows service for a certain purpose. We were glad to find out that Visual Studio allows you to easily create one of those and install it using the installutil command.
When we tried to debug our service, which was running on our local Windows 2000 systems, we ran into some problems. The first one is well known - you can't debug the service unless it is started already*, so how do you debug the entrance to the service - the OnStart method? There are many solutions for this, we solved it with code similar to this:
protected override void OnStart(string[] args)
{
new Thread(
delegate()
{
#if DEBUG
Thread.Sleep(20000);
#endif
StartService();
}
).Start();
}
That is, we start the service in a new thread so that the OnStart method will return right away. In debug, we stall it for 20 seconds so that we can attach the debugger.
So far, so good. But we hit another road block soon enough - when attaching the debugger to the service process, our breakpoints had the familiar "the breakpoint will currently not be hit" symbol, which means the debug symbols had not been loaded, for some reason.
After a lot of head scratching we realized the cause: the user that the service was running under was a weak user that we created. When we added the user to the admin group, the debugging worked perfectly. That is of course, a problem - we do not want our user to have admin privileges, not even in development (we can definitely expect surprises when the user becomes weaker in production). So far, we haven't found a solution for this.
Interestingly, this seems to be a Windows 2000 issue. I tried doing the same thing at home with Windows XP Pro, and the problem did not return (that is, I managed to debug a weak user service). I could not find any reference to this problem online, which led me to think we might be missing something, although I can't figure out what.
I'll keep you posted when we find the solution.
________________________________
* Actually, you can debug it if you can attach the debugger to the process quickly enough, but since the OS requires that the OnStart method return in under 30 seconds, that doesn't leave you with much time for debugging...