December 5, 2007
Windows Service Development Woes

At work, I’ve been working on a Windows service using C#, and I encountered a couple of problems that took a bit of digging to solve (for every page with useful info there are many pages with people asking how to solve the problem). I thought I’d put those here so they would be in one place for others who run into the same annoyances.


1) How do I debug my Windows service startup?

Here is how to do it. There are so many things buried in the .NET framework that its easy to overlook some of the most obvious and useful things, particularly when we are used to having a robust IDE like VS to take care of us. In this case, a single line of code…

System.Diagnostics.Debugger.Launch();

..brings up a “Select debugger” window, so that you can attach a debugger BEFORE the OnStart() method of your service is invoked.

2. After uninstalling my Windows service, I can’t reinstall it. I get an error saying “Source MYSERVICENAME already exists on the local computer.” What’s up with that?

Dude, I still don’t know what’s up with that, but I figured out how to fix it. Lot’s of people online were saying to go to a console and run “sc delete SERVICENAME” and that sort of thing, but in my case “sc query” didn’t even list my servce, and it just stated the obvious when I ran “sc delete”, which is that the service wasn’t installed.

In the end, I just deleted any references to SERVICENAME from the registry under HKEY_LOCAL_MACHINE\SYSTEM\*\EventLog\*, where * can be a couple of different things. Just search for your service name and ‘F3′ through the hits.

No Comments »
There are no comments for this article.

Leave a comment