Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

c# - create dependency between windows services startup

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...