Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set service dependencies after install

I have an application that runs as a Windows service. It stores various things settings in a database that are looked up when the service starts. I built the service to support various types of databases (SQL Server, Oracle, MySQL, etc). Often times end users choose to configure the software to use SQL Server (they can simply modify a config file with the connection string and restart the service). The problem is that when their machine boots up, often times SQL Server is started after my service so my service errors out on start up because it can't connect to the database. I know that I can specify dependencies for my service to help guide the Windows service manager to start the appropriate services before mine. However, I don't know what services to depend upon at install time (when my service is registered) since the user can change databases later on.

So my question is: is there a way for the user to manually indicate the service dependencies based on the database that they are using? If not, what is the proper design approach that I should be taking? I've thought about trying to do something like wait 30 seconds after my service starts up before connecting to the database but this seems really flaky for various reasons. I've also considered trying to "lazily" connect to the database; the problem is that I need a connection immediately upon start up since the database contains various pieces of vital info that my service needs when it first starts. Any ideas?

like image 687
Dennis Avatar asked May 17 '10 16:05

Dennis


People also ask

How do you add dependency on a Windows service after the service is installed?

You can add service dependencies by adding the "DependOnService" value to the service in the registry using the regedit command, services can be found under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<Service name> .

How do Windows service dependencies work?

In many instances, Windows services require other system components to run, before they can start operating. These components are known as dependencies and if they fail to start themselves, then the services that require them also fail to start.

How can we define dependency on services?

Service dependencies are an advanced feature that allow you to control the behavior of services based on the status of one or more other services. More specifically, you can repress the execution of service checks and notifications for services if various criteria that you specify are met.


2 Answers

Dennis what your looking for is SC.exe. This is a command line tool that users can use to configure services.

sc [Servername] Command Servicename [Optionname= Optionvalue...]

more specificly you would want to use

sc [ServerName] config ServiceName depend=servicetoDependOn

Here is a link on the commandlike options for SC.EXE http://msdn.microsoft.com/en-us/library/ms810435.aspx

like image 180
John Hartsock Avatar answered Oct 03 '22 05:10

John Hartsock


A possible (far from ideal) code solution:

In you startup method code it as a loop that terminates when you've got a connection. Then in that loop trap any database connection errors and keep retrying as the following pseudo code illustrates:

bool connected = false;
while (!connected)
{
    try
    {
        connected = openDatabase(...);
    }
    catch (connection error)
    {
         // It might be worth waiting for some time here
    }
}

This means that your program doesn't continue until it has a connection. However, it could also mean that your program never gets out of this loop, so you'd need some way of terminating it - either manually or after a certain number of tries.

As you need your service to start in a reasonable time, this code can't go in the main initialisation. You have to arrange for your program to "start" successfully, but not do any processing until this method had returned connected = true. You might achieve this by putting this code in a thread and then starting your actual application code on the "thread completed" event.

like image 27
ChrisF Avatar answered Oct 03 '22 05:10

ChrisF