开发者

create dependency between windows services startup

开发者 https://www.devze.com 2023-02-14 08:53 出处:网络
I have created a windows service which is set to start automatically. This service connects to the database service on startup. The issue is the database service seems to start after my service. Is th

I have created a windows service which is set to start automatically. This service connects to the database service on startup. The issue is the database service seems to start after my service. Is there is a programmatic way to define this dependency so that my service starts after the database service has started.

I found this article http://www.boyce.us/windows/servertipcontent.asp?ID=7 which talks about adding a registry entry to do that. I would like to know if there is a C# way to do this?

Update:

Adding to the above question. Here is another scenario. The services are being installed using installshied which does not need a projectinsaller. It seems installshield looks for 开发者_开发百科classes deriving from ServiceBase class and installs each service. How to add the dependency in such a scenario?


You're looking for the ServiceInstaller.ServicesDependedOn Property for your project's ServiceInstaller component.

From the article's Remarks section (and I bolded the part you're interested in):

A service can require other services to be running before it can start. The information from this property is written to a key in the registry. When the user (or the system, in the case of automatic startup) tries to run the service, the Service Control Manager (SCM) verifies that each of the services in the array has already been started.

If any service in the array is not running then, the SCM tries to start them. This includes services with Manual StartType.

If any service upon which this service depends fails to start, this service will not start. An exception is not thrown if the system is not started because there is no exception handling at the system level to detect this. Decide how to handle service start failures and implement this in your code. Typically, a dialog appears to the user at startup if a service fails to start.

If the service does not start, an entry is written to the Application event log.

The services upon which this service depends do not need to be in the same executable.


In addition to Jay Riggs' answer, here's and example of what you should add to the serviceinstaller to make your service dependent on the eventlog

Me.ServiceInstaller1.ServiceName = "Service1";
Me.ServiceInstaller1.ServicesDependedOn = new string[] {"EventLog"};

Off course, if you have another service dependency, change the 'Eventlog' to something else..


I use the advapi32.dll as I have full control over the installation. My services register themselves, set descriptions (though I use sc.exe), set dependencies, set restart on failures (also using sc.exe) etc.

ChangeServiceConfig2 API is supposed to set descriptions, but doesn't seem to work in .NET

The dependency is set by the CreateService API..

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern UIntPtr CreateService(UIntPtr SC_HANDLE, string lpSvcName, string lpDisplayName,
        uint dwDesiredAccess, uint dwServiceType, uint dwStartType, uint dwErrorControl, string lpPathName,
        string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);
0

精彩评论

暂无评论...
验证码 换一张
取 消