【发布时间】:2010-11-06 13:21:52
【问题描述】:
我的安装程序不支持安装服务,但我可以运行程序/命令行等,所以我的问题是如何安装 Windows 服务并使用命令行添加 2 个依赖项?该程序是一个 .Net 2.0 应用程序。
谢谢
【问题讨论】:
-
你用的是什么安装系统?
标签: .net windows windows-services .net-2.0 installation
我的安装程序不支持安装服务,但我可以运行程序/命令行等,所以我的问题是如何安装 Windows 服务并使用命令行添加 2 个依赖项?该程序是一个 .Net 2.0 应用程序。
谢谢
【问题讨论】:
标签: .net windows windows-services .net-2.0 installation
您可以编写一个自安装服务,并让它设置您的服务在执行安装程序时所依赖的服务列表。
基本步骤:
编辑:忘了提到你可以使用例如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
Above 表示在 ServiceB 启动之前,ServiceA 不会启动。如果停止 ServiceB,ServiceA 会自动停止。
sc config ServiceA depend= ServiceB/ServiceC/ServiceD
Above 表示在 ServiceB、ServiceC 和 ServiceD 都启动之前,ServiceA 不会启动。如果您停止 ServiceB、ServiceC 或 ServiceD 中的任何一个,ServiceA 将自动停止。
sc config ServiceA depend= /
sc qc ServiceA
【讨论】:
一种可用的方法是 sc.exe。它允许您从命令提示符安装和控制服务。这是一个older article,涵盖了它的用途。它还允许您指定依赖项。
查看文章中的 sc create 部分,了解您需要的内容。
【讨论】:
在 codeproject 上有一个动态安装程序项目,我发现它通常对服务安装很有用。
【讨论】:
Visual Studio Setup/Deployment 项目为此工作。它们不是最好的安装程序引擎,但它们适用于简单的场景。
【讨论】: