服务启动之间创建依赖关系

服务启动之间创建依赖关系

本文介绍了在 Windows 服务启动之间创建依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我发现这篇文章 http://www.boyce.us/windows/servertipcontent.asp?ID=7 谈到添加一个注册表项来做到这一点.我想知道是否有 C# 方法可以做到这一点?

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?

更新:

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

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?

推荐答案

您正在寻找 ServiceInstaller.ServicesDependedOn 属性,用于您项目的 ServiceInstaller 组件.

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):

一个服务可能需要其他服务在它可以启动之前运行.这来自这个属性的信息是写入注册表中的一个键.什么时候用户(或系统,在这种情况下自动启动)尝试运行服务,服务控制管理器(SCM) 验证每个阵列中的服务已经开始了.

如果阵列中的任何服务不是然后运行,SCM 尝试启动他们.这包括具有以下功能的服务手动启动类型.

如果此服务所依赖的任何服务依赖无法启动,此服务不会启动.例外不是如果系统未启动则抛出因为没有异常处理在系统级别检测到这一点.决定如何处理服务启动失败并在您的代码.通常,会出现一个对话框如果服务失败,启动时的用户开始.

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.

这篇关于在 Windows 服务启动之间创建依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:31