Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create dependency between windows services startup

Tags:

I have created a windows service which is set to start automatically. This service connects to the database service on startup. The issue is the database service seems to start after my service. Is there is a programmatic way to define this dependency so that my service starts after the database service has started.

I found this article http://www.boyce.us/windows/servertipcontent.asp?ID=7 which talks about adding a registry entry to do that. I would like to know if there is a C# way to do this?

Update:

Adding to the above question. Here is another scenario. The services are being installed using installshied which does not need a projectinsaller. It seems installshield looks for classes deriving from ServiceBase class and installs each service. How to add the dependency in such a scenario?

like image 321
stackoverflowuser Avatar asked Mar 01 '11 19:03

stackoverflowuser


2 Answers

You're looking for the ServiceInstaller.ServicesDependedOn Property for your project's ServiceInstaller component.

From the article's Remarks section (and I bolded the part you're interested in):

A service can require other services to be running before it can start. The information from this property is written to a key in the registry. When the user (or the system, in the case of automatic startup) tries to run the service, the Service Control Manager (SCM) verifies that each of the services in the array has already been started.

If any service in the array is not running then, the SCM tries to start them. This includes services with Manual StartType.

If any service upon which this service depends fails to start, this service will not start. An exception is not thrown if the system is not started because there is no exception handling at the system level to detect this. Decide how to handle service start failures and implement this in your code. Typically, a dialog appears to the user at startup if a service fails to start.

If the service does not start, an entry is written to the Application event log.

The services upon which this service depends do not need to be in the same executable.

like image 143
Jay Riggs Avatar answered Sep 21 '22 14:09

Jay Riggs


In addition to Jay Riggs' answer, here's and example of what you should add to the serviceinstaller to make your service dependent on the eventlog

Me.ServiceInstaller1.ServiceName = "Service1";
Me.ServiceInstaller1.ServicesDependedOn = new string[] {"EventLog"};

Off course, if you have another service dependency, change the 'Eventlog' to something else..

like image 35
real_yggdrasil Avatar answered Sep 23 '22 14:09

real_yggdrasil