gpt4 book ai didi

c# - 在 Windows 服务启动之间创建依赖关系

转载 作者:IT王子 更新时间:2023-10-29 04:30:37 24 4
gpt4 key购买 nike

我创建了一个设置为自动启动的 Windows 服务。此服务在启动时连接到数据库服务。问题是数据库服务似乎在我的服务之后启动。是否有一种编程方式来定义此依赖关系,以便我的服务在数据库服务启动后启动。

我找到了这篇文章 http://www.boyce.us/windows/servertipcontent.asp?ID=7其中讨论了添加注册表项来执行此操作。我想知道是否有 C# 方法可以做到这一点?

更新:

添加到上述问题。这是另一种情况。正在使用不需要 projectinsaller 的 installshied 安装服务。似乎 installshield 寻找从 ServiceBase 类派生的类并安装每个服务。这种场景如何添加依赖?

最佳答案

您正在寻找 ServiceInstaller.ServicesDependedOn Property用于您项目的 ServiceInstaller 组件。

来自文章的备注部分(我将您感兴趣的部分加粗):

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.

关于c# - 在 Windows 服务启动之间创建依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5159257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com