Nasty problems with C# Windows Service
I have been building a Windows Service in .NET 2.0 using C#, and I have come across a particularly nasty problem. I have been developing the service for a while, and recompiling, installing and running the service after every change iteration for testing without any problems. This morning, however, I noticed that suddenly the service would no longer start - as soon as I tried to start it, I would get a message saying - “The service … has stopped. Some services stop when they have no work to do … etc”.
Now, this service definitely has “work to do”, so it seemed strange when it first happened. I rolled back the code to a previous version, but still no luck - now even previous versions of the service that worked before would no longer start. I checked the Event Logs - nothing. I checked the install process output - installing (and uninstalling) the service was still error free. I tried a restart of the entire OS - no luck. So here I was with a service that would not start, no matter what I did - and no obvious way of debugging it or working out what was going on!?
Well, it wasted a lot of time for me, but I finally worked it out through trial and error. It turns out that the Event Log was full. I had Event Log messages writes sprinkled throughout my code, and they were now failing and throwing exceptions. What’s more, I had a try ... catch block that was trying to write out an error message in the catch block itself, for debugging purposes. All of this basically conspired to send the service into a silent meltdown. The solution is to a) clear the Event log and b) remove any Event log writes from catch blocks prior to production.
