安装具有依赖项的 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1055942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing a Windows Service with dependencies
提问by VolkerK
My installer program doesn't suppport installing services but I can run a program/command line etc so my question is how can I install a Windows Service and add 2 dependencies using the command line? The program is a .Net 2.0 app.
我的安装程序不支持安装服务,但我可以运行程序/命令行等,所以我的问题是如何安装 Windows 服务并使用命令行添加 2 个依赖项?该程序是一个 .Net 2.0 应用程序。
Thanks
谢谢
回答by VolkerK
You can write a self-installing service and have it set a list of services your service depends on when the installer is executed.
您可以编写一个自安装服务,并让它设置您的服务在执行安装程序时所依赖的服务列表。
Basic steps:
基本步骤:
- Add a reference to System.Configuration.Install to your project.
- Add a class that derives from System.Configuration.Install.Installerand has the RunInstaller attributeapplied.
- In its constructor create both a ServiceProcessInstallerand a ServiceInstallerobject.
- On the ServiceInstaller object you mark all the dependencies you want/need with the ServicesDependedOnproperty.
- Add these two installers to the InstallersCollectionyour installer inherited from System.Configuration.Install.Installer
- done.
- 向您的项目添加对 System.Configuration.Install 的引用。
- 添加一个派生自System.Configuration.Install.Installer并应用了RunInstaller 属性的类。
- 在它的构造函数中创建一个ServiceProcessInstaller和一个ServiceInstaller对象。
- 在 ServiceInstaller 对象上,您使用ServicesDependedOn属性标记您想要/需要的所有依赖项。
- 将这两个安装程序添加到安装程序从 System.Configuration.Install.Installer 继承的InstallersCollection
- 完毕。
edit: forgot to mention that you can use e.g. Installutil.exeto invoke the installer.
编辑:忘记提及您可以使用例如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);
}
}
}
}
回答by Kip
This can also be done via an elevated command prompt using the sc
command. The syntax is:
这也可以通过使用sc
命令的提升的命令提示符来完成。语法是:
sc config [service name] depend= <Dependencies(separated by / (forward slash))>
Note: There is a space afterthe equals sign, and there is notone before it.
注:有一个空间后等号,有没有一个之前。
Warning: depend=
parameter will overwriteexisting dependencies list, not append. So for example, if ServiceA already depends on ServiceB and ServiceC, if you run depend= ServiceD
, ServiceA will now depend onlyon ServiceD.
警告:depend=
参数将覆盖现有的依赖项列表,而不是追加。例如,如果 ServiceA 已经依赖于 ServiceB 和 ServiceC,如果您运行depend= ServiceD
,ServiceA 现在将只依赖于 ServiceD。
Examples
例子
Dependency on one other service:
对其他服务的依赖:
sc config ServiceA depend= ServiceB
Above means that ServiceA will not start until ServiceB has started. If you stop ServiceB, ServiceA will stop automatically.
以上意味着 ServiceA 不会启动,直到 ServiceB 启动。如果停止ServiceB,ServiceA 将自动停止。
Dependency on multiple other services:
对多个其他服务的依赖:
sc config ServiceA depend= ServiceB/ServiceC/ServiceD
Above means that ServiceA will not start until ServiceB, ServiceC, and ServiceD have all started. If you stop any of ServiceB, ServiceC, or ServiceD, ServiceA will stop automatically.
上面的意思是ServiceA在ServiceB、ServiceC、ServiceD都启动后才会启动。如果您停止 ServiceB、ServiceC 或 ServiceD 中的任何一个,ServiceA 将自动停止。
To remove all dependencies:
要删除所有依赖项:
sc config ServiceA depend= /
To list current dependencies:
列出当前的依赖项:
sc qc ServiceA
回答by Joe Doyle
One method that's available is sc.exe. It allows you to install and control services from a command prompt. Here is an older articlecovering it's use. It does allow you to specify dependencies as well.
一种可用的方法是 sc.exe。它允许您从命令提示符安装和控制服务。这是一篇较旧的文章,介绍了它的使用。它也允许您指定依赖项。
Take a look at the article for the sc createportion for what you need.
查看有关sc create部分的文章,了解您需要的内容。
回答by Chris Morley
There is a dynamic installer project on codeproject that I have found useful for services installation, in general.
codeproject 上有一个动态安装程序项目,我发现它通常对服务安装很有用。
回答by MattH
Visual Studio Setup/Deploymentprojects work for this. They are not the best installer engine, but they work fine for simple scenarios.
Visual Studio安装/部署项目适用于此。它们不是最好的安装程序引擎,但它们在简单的场景中工作得很好。