Specifying Windows service dependencies in app.config?
I'm creating a Windows service w开发者_运维问答hich will depend on the SQL Server service having started up before mine. I know you can add such a dependency from code, the Windows GUI, or the command line via sc
, but is it possible to specify such dependencies in the service's app.config
? Are there any other ways to setup service dependencies?
As far as the app.config is concerned, there is nothing you can put in there that will delay the starting of your service until after another service is running. You have to do this at the OS level (sc
or registry), not at the service level (app.config).
In general, though, writing your service with the expectation that another service is running makes your service fragile. What if the local system admin deletes SQL Server from the machine or configures it not to run by default? In either of these two cases, your service will never run.
Instead, I would suggest that you write your service in such a way that it does not depend on another service to start, even if it requires that service in order to do any work. For example, if your service queries the database based on a remote user request and SQL Server is not running at that time, throw an exception that can then be displayed to the user.
Alternatively, when you install your service, add the dependency on SQL Server at that point if you can reasonably expect SQL Server to be there.
精彩评论