Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
4.80/5 (4 votes)
See more:
This is not a question, but is too short to be an article. Learn from my mistake.

Yesterday, I was testing a windows service I wrote using Visual Studio 2008 on a Windows 2000 machine. I had originally targeted .Net 3.5 when I created the project, and had to re-target the assemblies to 2.0 before installing it on the target machine. When I installed it, the event log would say that the service started successfully, but a few seconds later, I would get an error event - an invalid operation exception.

I removed EVERYTHING from the service except a keep-alive thread so it would keep running once installed, and it still threw the exception.

Thinking it might e a flaw in VS2008 when changing from .Net 3.5 to .Net 2.0, I tried creating an entirely new solution targeting .Net 2.0 from the start. It still threw the exception. And then I had an epiphany.

The installer class sets the name of the service. One thing that all windows services do (when you create them with the VS2008 template) is calls the InitializeComponent() method. I had always deleted this call in prior services I had written since it seemed like a pointless thing to do, but this time, I had left the call in. In InitializeComponent, it tries to set the service name to "Service1". This is NOT the name I gave it in the installer object. After changing the name to the same thing I used in the Installer object, it worked just fine - no exceptions.

When I changed the name of the service.cs file, I guess it missed that string in the re-factoring process. I spent four hours on this problem. BTW, the other fix for this would have been to NOT call InitializeComponent.

[EDIT] BTW, I had tested this service on my own Vista box, and it worked fine. While I was waiting for access to the Win2K box, I figured I'd fix the Service name (because it was showing up as "Service1" in the event log). I re-factored and compiled, but did not re-test it before trying it on the Win2K box (I figured the re-factor would have done what it was supposed to do). The lesson here? ALWAYS RE-TEST, NO MATTER HOW MINOR THE CHANGE IS PERCEIVED TO BE. This is proof that even the programmers with 30 years in the industry make the most rookie kind of mistakes every once in a while.

Posted
Updated 19-Mar-10 2:14am
v4

1 solution

John Simmons / outlaw programmer wrote:
This is proof that even the programmers with 30 years in the industry make the most rookie kind of mistakes every once in a while.


Ha. Rookie [laugh]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900