Thursday, 6 September 2018

Windows Service - Add Dependency - After Service is Installed

I have a Windows service, that has a dependency on MSMQ (Message Queuing). So I want my service to start only after MSMQ service is started completely. Else my service will run into some anomalies.

A way to do it is to add the dependency in the service install, but this dependency came later on. So now I need to add the dependency post install.

You can do it via an elevated comand prompt using sc command. Follow the below syntax:

sc config [service name] depend= <dependencies by="" forward="" separated="" slash="">

Note 1 : Retain the space after the depend=. This is important.
Note 2 : depend= will overwrite and not append to existing dependencies list. So if you have multiple dependencies, remember to add all the required dependencies while trying to add new one as well.

Example 1:
Below command will add a dependency of service Service2 to service Service1. That means Service1 will start only after Service2 starts. Also Service1 will stop if Service2 is stopped.
sc config Service1 depend= Service2
Example 2:
Command for dependency on multiple services is added below. Here Service1 will not start until Service2, Service3, Service4 are started. Also if any of the dependent services are stopped Service1 will stop.
sc config Service1 depend= Service2/Service3/Service4
Example 3:
Remove all dependencies:
sc config Service1 depend= /
Example 4:
List all dependencies:
sc qc Service1