I've a credit card applicatoin that runs like a Home windows service. It stores something more important configurations inside a database which are researched once the service begins. I built the plan to support various databases (SQL Server, Oracle, MySQL, etc). Frequently occasions clients decide to configure the program to make use of SQL Server (they are able to simply modify a config file using the connection string and restart the service). However , when their machine boots up, frequently occasions SQL Server is began after my service so my service errors on launch since it can't connect with the database. I understand will be able to specify dependencies for my plan to help guide to the Home windows service manager to begin the right services before mine. However, I'm not sure what services to rely on at install time (when my services are registered) because the user can alter databases afterwards.

So my real question is: it is possible to method for the consumer to by hand indicate the service dependencies in line with the database that they're using? Otherwise, what's the proper design approach which i ought to be taking? I have considered trying to behave like wait thirty seconds after my service begins before hooking up towards the database but this appears really flaky for a number of reasons. I have also considered attempting to "lazily" connect with the database however , I want an association immediately upon launch because the database consists of various bits of vital info that my service needs if this first begins. Any ideas?

Dennis what your searching for is SC.exe. This can be a command line tool that customers may use to configure services.

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

more specificly you would like to use

sc [ServerName] config ServiceName depend=servicetoDependOn

This is a link around the commandlike choices for SC.EXE http://msdn.microsoft.com/en-us/library/ms810435.aspx

A potential (not even close to ideal) code solution:

In your soul startup method code it as being a loop that terminates when you have an association. Then for the reason that loop trap any database connection errors and retrying because the following pseudo code demonstrates:

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

Which means that your program does not continue until it features a connection. However, it might also imply that your program never will get using this loop, so you'd take some method of terminating it - either by hand or following a certain quantity of tries.

Since you need your merchandise to begin inside a reasonable time, this code can't use the primary initialisation. You need to request your program to "start" effectively, although not inflict processing until this process had came back connected = true. You may accomplish this by putting this code inside a thread after which beginning your actual application code around the "thread completed" event.

Not really a direct answer put some things you are able to consider

  1. Home windows service could be began Instantly having a delay. You should check this in SO for many details about it.

How to make Windows Service start as “Automatic (Delayed Start)”

  1. Take a look publish How to: Code Service Dependencies