Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
Rob Cooke Avatar asked Jul 27 '10 17:07

Rob Cooke


People also ask

Why is my Windows Service not starting?

Why will my Windows Service not start? Make sure that the service's dependencies are running. Make sure that the service is not set to DISABLED. Sometimes when the service is set to log on as the local system account (default) for some reason, perhaps local permissions, it is unable to launch.

What is Windows Service in .NET Core?

An ASP.NET Core app can be hosted on Windows as a Windows Service without using IIS. When hosted as a Windows Service, the app automatically starts after server reboots.


2 Answers

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.

like image 182
Rob Cooke Avatar answered Sep 28 '22 09:09

Rob Cooke


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.

like image 45
msarchet Avatar answered Sep 28 '22 09:09

msarchet