Rob Cooke
Rob Cooke

Reputation: 978

Strange Problem with a .NET Windows Service

I have two Windows services written in C# following the same patterns and methodology.

Both services were development tested against a Windows 7 VM and QA tested on Windows Server 2008 VM. Both services have been installed and uninstalled many times under these test environments without issue, however upon installing in the production environment (Windows Server 2008) one of the two services refuses to start.

To install the services we are using InstallUtil.exe with ServiceInstaller and ServiceProcessInstaller components attached to the service.

By all appearances, the failing service installs successfully. InstallUtil.exe reports success and the service appears in the Services snapin. You can also locate the service in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Blah Blah. However, if you attempt to start the service you get the following:

net start blah.blah.exe "The service name is invalid."

...or going through the Services snapin... "Windows could not start the "Blah Blah" service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion."

I have added some event logging to the constructor of the service class failing service, but it does not appear to get called.

As this is a production box, there is no Visual Studio on the box and remote debugging is out of the question.

Is there any other way for me to gain debugging info on why the failing service isn't starting?

Is there any other obvious-ish reason that I might see this kind of issue?

Edit: I should have also mentioned.. The only other evidence of a problem in the Windows Event Viewer is two messages in the System log from the Service Control Manager:

"A timeout was reached (30000 milliseconds) while waiting for the Blah Blah service to connect.

"The Blah Blah service failed to start due to the following error: The service did not respond to the start or control request in a timely fashion."

Edit: Resolved The issue ended up being a combination of a configuration mistake and a bug that was hiding it. See my answer below for more details.

Upvotes: 11

Views: 12047

Answers (6)

Deshan
Deshan

Reputation: 2132

It is possible for this error to occur when

  • your service is depending on another service/application
  • or due to invalid app.config configurations

Encountered the same issue when Entity Framework was unable to connect to the database server due to lack of permission. Better to add a global level error logging mechanism to your application to debug the issue easily.Event Viewer might not reveal exact details in some scenarios.

Upvotes: 0

Rob Cooke
Rob Cooke

Reputation: 978

Jeopardy Answer: "How might invalid custom configuration combined with a bad global exception handler manifest itself in a .NET Windows service?"

Figured it out.

The root cause of the problem was an invalid custom configuration section in the app.config. We use a custom configuration section to configure the service from the app.config and the assembly and namespace of the ConfigurationSection derived class had changed recently.

As it turns out, our production configuration was looking for the definition of the custom ConfigurationSection in the wrong assembly and the exception thrown when failing to instantiate it was getting hidden by a bug where exceptions caught early in the life of the service would attempt to be logged to the custom log rather than the Application event log. (Since the event log source did not exist on the custom event log, this would throw another exception from the global exception handler and the service would die in the constructor.)

This second exception did not get logged anywhere and we only found it through code inspection.

The resolution was to fix the configuration and to modify the global exception handler to only attempt to write to the Application event log using the service name as the event log source. (InstallUtil registers the service name as an event log source on the Application log.)

Thanks for the help everyone! Sorry this particular issue ended up being so specific to our setup.

Upvotes: 8

Brandi
Brandi

Reputation: 1579

I've run into problems like this many times while programming my own services, so I'll just list out a bunch of things that at various points solved my problems and hope that they help you:

  1. I've had to restart services.msc because I've uninstalled a service it still thought it had a reference to. However, when I would start it, the "service was invalid"

  2. If you're making the service from a console application (so that it can be debugged) but forget to change it back to a service, it won't start.

  3. When I was using InstallUtil.exe, sometimes it would attempt to install multiple copies, so I switched to just using a Setup Project.

Hope that helps in some way.

Upvotes: 1

msarchet
msarchet

Reputation: 15242

It could be possible from the error message that you've described

net start blah.blah.exe "The service name is invalid."

That the name that you gave the service in the service install component that you added in visual studio is not what you think it is.

I've had this problem quite a few times with developers misnaming services in installs.

Upvotes: 2

ajay_whiz
ajay_whiz

Reputation: 17931

what are you trying to do on service start?

also check the account through which the service is running and the account has the necessary privileges.

Upvotes: 1

Zesty
Zesty

Reputation: 2991

This is a common issue. What code do you have in your Start event?

You should only have code that activates a Timer when the service starts. This allows the Start event to complete quickly and notify the controller. If the execution takes longer than that, you will get the error you received. There might be some reason (possibly data-related) why its taking longer in production.

When the Timer ticks, execute your code and stop the Timer. Obviously, also put everything in try/catch and log the exceptions.

Upvotes: 0

Related Questions