Define service dependencies after installation

I have an application that runs as a windows service. It stores various parameter settings in the database, which are viewed when the service starts. I built a service to support various types of databases (SQL Server, Oracle, MySQL, etc.). Often end users choose the software configuration to use SQL Server (they can simply change the configuration file with the connection string and restart the service). The problem is that when their machine boots up, often from the moment SQL Server starts after my service, so my service errors occur at startup because it cannot connect to the database. I know that I can specify dependencies for my service to help Windows Manager start the corresponding services in front of mine. However, I do not know which services should depend on the installation (when my service is registered), since the user can subsequently change the databases.

So my question is: is there a way for a user to manually specify service dependencies based on the database used? If not, what is the right design approach I should take? I thought about trying to do something like 30 minutes after starting my service before connecting to the database, but it looks different in different ways. I also thought about trying to "lazily" connect to the database; the problem is that I need a connection right after launch, since the database contains various pieces of vital information that I need when I first start. Any ideas?

+6
c # database windows-services
source share
3 answers

Dennis you need SC.exe. This is a command line tool that users can use to configure services.

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

more specifically you would like to use

 sc [ServerName] config ServiceName depend=servicetoDependOn 

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

+3
source share

Possible (far from ideal) code solution:

In your autoload method, it's like a loop that ends when you have a connection. Then in this loop are placed any errors connecting to the database and continue to try again in the form of the following pseudo-code:

 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 will not continue until a connection is established. However, this may also mean that your program will never exit this cycle, so you will need some way to terminate it - either manually or after a certain number of attempts.

Since you need your service to start in a reasonable amount of time, this code cannot be included in the main initialization. You must arrange for your program to run successfully, but do not perform processing until this method returns connected = true . You can achieve this by putting this code in a thread and then running your actual application code in the thread completed event.

+1
source share

The immediate answer puts some points that you can look into

  • Windows service may be started automatically with a delay. You can check this question in SO for some info on this.

How to start a Windows service as "Automatic (Delayed Start)"

  1. Bookmark this post Like: Code Service Dependencies
0
source share

All Articles