安装依赖的Windows服务

我的安装程序不支持安装服务,但我可以运行一个程序/命令行等,所以我的问题是如何安装Windows服务,并添加2依赖使用命令行? 该程序是一个.Net 2.0应用程序。

谢谢

你可以编写一个自我安装的服务,让它设置你的服务在安装程序执行时依赖的服务列表。

基本步骤:

  • 将对System.Configuration.Install的引用添加到您的项目。
  • 添加派生自System.Configuration.Install.Installer的类并应用了RunInstaller属性 。
  • 在其构造函数中创建一个ServiceProcessInstaller和一个ServiceInstaller对象。
  • 在ServiceInstaller对象上,使用ServicesDependedOn属性标记所需的所有依赖关系。
  • 将这两个安装程序添加到您的安装程序继承自System.Configuration.Install.Installer的InstallersCollection
  • 完成。

编辑:忘了提及,你可以使用例如Installutil.exe来调用安装程序。

[RunInstaller(true)] public class MyServiceInstaller : Installer { public MyServiceInstaller() { using ( ServiceProcessInstaller procInstaller=new ServiceProcessInstaller() ) { procInstaller.Account = ServiceAccount.LocalSystem; using ( ServiceInstaller installer=new ServiceInstaller() ) { installer.StartType = ServiceStartMode.Automatic; installer.ServiceName = "FooService"; installer.DisplayName = "serves a lot of foo."; installer.ServicesDependedOn = new string [] { "CLIPBOOK" }; this.Installers.Add(procInstaller); this.Installers.Add(installer); } } } } 

这也可以通过提升命令提示符使用sc命令来完成。 语法是:

 sc config [service name] depend= <Dependencies(separated by / (forward slash))> 

:等号后面有空格,前面没有空格。

警告depend=参数将覆盖现有的依赖项列表,而不是追加。 例如,如果ServiceA已经依赖于ServiceB和ServiceC,那么如果运行depend= ServiceD ,则ServiceA现在将依赖于ServiceD。

例子

依赖另一项服务:

 sc config ServiceA depend= ServiceB 

上面的意思是ServiceB在ServiceB启动之前不会启动。 如果停止ServiceB,ServiceA将自动停止。

依赖多个其他服务:

 sc config ServiceA depend= ServiceB/ServiceC/ServiceD 

上面的意思是,ServiceA,ServiceC和ServiceD都启动之前,ServiceA不会启动。 如果您停止任何ServiceB,ServiceC或ServiceD,ServiceA将自动停止。

要删除所有依赖关系:

 sc config ServiceA depend= / 

列出当前的依赖关系:

 sc qc ServiceA 

一种可用的方法是sc.exe。 它允许您从命令提示符安装和控制服务。 这是一个覆盖它的旧文章 。 它也允许你指定依赖关系。

看看sc创建部分的文章,你需要什么。

在codeproject上有一个动态的安装程序项目,通常我发现它对于服务安装很有用。

Visual Studio 安装/部署项目为此工作。 他们不是最好的安装程序引擎,但他们在简单的情况下工作正常。