.net 使用 SC.exe 或 InstallUtil.exe 安装 Windows 服务 - 有区别,但哪个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4692250/
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 windows service with SC.exe or InstallUtil.exe - there is difference but which?
提问by LosManos
SC.exeand InstallUtilboth install/uninstall windows services. But they don't seem to work the same way.
SC.exe和InstallUtil都安装/卸载 Windows 服务。但它们的工作方式似乎不同。
What is the difference?
有什么不同?
For instance InstallUtilfails (some file or dependency not found error) while Sc createhappily installs the service. Too add to the strangeness; the service doesn't show up if I run net startin the console. But it does show up in the services GUI. Variants of this happen when I try to uninstall.
例如InstallUtil失败(未找到某些文件或依赖项错误)而Sc create愉快地安装服务。太增加了陌生感;如果我在控制台中运行net start,则该服务不会显示。但它确实显示在服务 GUI 中。当我尝试卸载时会发生这种情况。
I have written the service myself and earlier versions work. Dotnet3.5.
我自己编写了该服务,并且早期版本可以工作。dotnet3.5。
采纳答案by Hans Passant
Yes, installing a service isn't particularly complicated. It just takes writing a handful of registry keys. You can have a look-see with Regedit.exe, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services.
是的,安装服务并不是特别复杂。只需要编写一些注册表项。您可以使用 Regedit.exe 进行查看,导航到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services。
Sc.exe can write these keys too, using the supplied command line arguments. Nevertheless, this is not the right way to do it. The point of InstallUtil.exe is that it can activate custominstallation code. Code that the service author wrote. Which is not that uncommon, services tend to stuff config info in their registration keys for their own use. You'll see plenty of evidence for that when you have a look with Regedit.
Sc.exe 也可以使用提供的命令行参数编写这些密钥。然而,这不是正确的方法。InstallUtil.exe 的重点在于它可以激活自定义安装代码。服务作者编写的代码。这并不少见,服务倾向于在其注册密钥中填充配置信息供自己使用。当您查看 Regedit 时,您会看到大量证据。
回答by Kat Lim Ruiz
I prefer sc.exe over installutil.exe.
我更喜欢 sc.exe 而不是 installutil.exe。
InstallUtil forces you to add the dreadful ProjectInstaller class (I believe) and hardcode there the service name and service description.
InstallUtil 强制您添加可怕的 ProjectInstaller 类(我相信)并在那里硬编码服务名称和服务描述。
InstallUtil makes it very hard to put two versions of the same service running in the same machine at the same time.
InstallUtil 使得将同一服务的两个版本同时运行在同一台机器上变得非常困难。
That's why I just don't use InstallUtil.exe at all. Also because of previous responses: you need it to be in your deploy package. sc.exe is already in any Windows Xp and above (I believe).
这就是我根本不使用 InstallUtil.exe 的原因。也因为之前的回答:您需要将它放在您的部署包中。sc.exe 已经在任何 Windows Xp 及更高版本中(我相信)。
回答by Bizniztime
Main difference is that InstallUtilis not utility meant for service installation but as general installer tool. From MSDN pages you can see that:
主要区别在于InstallUtil不是用于服务安装的实用程序,而是作为通用安装程序工具。从 MSDN 页面您可以看到:
"The Installer tool is a command-line utility that allows you to install and uninstall server resources by executing the installer components in specified assemblies. This tool works in conjunction with classes in the System.Configuration.Install namespace."
“安装程序工具是一个命令行实用程序,允许您通过执行指定程序集中的安装程序组件来安装和卸载服务器资源。此工具与 System.Configuration.Install 命名空间中的类一起工作。”
So it can install service but it has many many many other benefits. Creating executables based on Installer Classgives you programatic control of whole installation/uninstallation procedure. ServiceInstallerand ServiceProcessInstaller, for instance, are used for service installation.
所以它可以安装服务,但它有很多很多其他的好处。基于安装程序类创建可执行文件 使您可以对整个安装/卸载过程进行编程控制。例如,ServiceInstaller和ServiceProcessInstaller用于服务安装。
'Sc' utility is used for service control and 'create' command will just create service based on chosen executable.
'Sc' 实用程序用于服务控制,'create' 命令将根据所选的可执行文件创建服务。
In your example
1. It is not meant to be installed with InstallUtiland error response should be quite clear about it.
2. InstallUtilfails because of a bug in installation code and using sc
createwill probably create a faulty service for you. Check into {exe_name}.InstallLogfor details.
在您的示例
1 中,它并不打算与InstallUtil一起安装,并且错误响应应该很清楚。
2. InstallUtil因安装代码中的错误而失败,使用sc create可能会为您创建一个错误的服务。查看{exe_name}.InstallLog了解详细信息。
回答by Cyryl P?otnicki
From the uninstall usage experience: sc.exe under windows 7 removes the entry from the list immediately, while after uninstalling with installutil there is a need for restart
从卸载使用体验来看:windows 7下的sc.exe会立即从列表中删除该条目,而使用installutil卸载后需要重新启动
回答by Joe the Coder
While InstallUtil is the preferred way to go with .NET services, one of it's shortcomings is it won't pick up binding redirects from your app.config, which in certain circumstances, can cause the install to fail. That's where using SC might gain you some benefit, at the expense of not being able to run code at install time.
虽然 InstallUtil 是使用 .NET 服务的首选方式,但它的缺点之一是它不会从 app.config 中获取绑定重定向,这在某些情况下会导致安装失败。这就是使用 SC 可能会给您带来一些好处的地方,但代价是无法在安装时运行代码。
Unfortunately for the OP, TopShelfdidn't exist at the time of his question. It works around the shortcomings of both SC and InstallUtil, and allows the service to start up with the debugger attached when starting it in Visual Studio. Plus, it's a lot easier to type myservice installthan have to drill down to the specific folder for InstallUtil, or type in a ton of parameters for SC.
不幸的是,对于 OP 来说,TopShelf在他提出问题时并不存在。它可以解决 SC 和 InstallUtil 的缺点,并允许服务在 Visual Studio 中启动时使用附加的调试器启动。另外,myservice install与必须深入到 InstallUtil 的特定文件夹或为 SC 输入大量参数相比,键入要容易得多。

