In this article, I will show how to set the dependencies on a Windows Service of a service or services through its registry or through Visual Studio.
Suppose after a reboot you need your service to be started automatically but many times we know certain services are required to be started before your service starts. In other words, your service is dependent on certain service(s) or we could say our service will not start until those services are started. Just for the sake of clarity, let us assume we have a Windows service or a WCF service (hosted on a Windows service) that checks its database connectivity on its startup. So before starting this service, the SQL Server Agent Service must be started. In this situation, Windows allows you to create service dependencies to take care of this. The Service dependency can be set either through the registry or through Visual Studio.
1. Service dependency setting through its Registry
Let us see how to set a service dependency through its registry. Even if you are not familiar with the registry at all, here is the step-by-step procedure to do this:
- Open the registry editor as in the following:
"Start" -> "Run" then enter "regedit" then click "OK"
- Navigate to the specific service you are trying to delay:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\
- If there is a "DependOnService" entry then simply modify it and add the service you want to start before the one you are editing in the registry. In this case, we want the service named 'Winmgmt' to be started before our service 'MyDemoService'.
But what if you don't find any entry for the "DependOnService" entry then you need to create a new one for this by right-clicking the service name then select "New" -> "Multi-string value".
Enter the name "DependOnService" then modify this new record and add the service name (for example Winmgmt) that you want to start before your service (for example MyDemoService). After successfully adding the dependent service name for your service, your service will be set as in the following image:
How to get Actual Service Name not Display Name
To add a prerequisite service against the 'DependOnService' entry, you need to ensure you have the actual service name instead of the service's display name. To get the actual service name use the following procedure:
- Open the Services Manager.
- Right-click the service in question, and choose properties.
- Use the Service name and not the Display name.
(For instance, the display name of the following service is "Windows Management Instrumentation" whereas the actual service name is "Winmgmt". See the images below for more clarity.)
- The server will require a restart to apply the change. Once restarted, you can open Services, right-click the service you edited, choose properties, then the Dependencies tab and you will see the change.
1. Service dependency setting through Visual Studio
To set service dependency through Visual Studio (2010), you need to open the 'ProjectInstaller' of the service and then open the properties of its 'Service Installer'. Add the dependent service name (in other words Winmgmt) by clicking the ellipses button. See the following images for more clarity:
Summary:
So in this article we learned how we add a service dependency for any given service through its registry or through Visual Studio.