我使用 Microsoft's walkthrough 创建了一个服务.
它有效。但我不明白怎么办。 Main()
方法似乎没有调用 ProjectInstaller
构造函数(它看起来像是安装服务的函数)。事实上,解决方案中的任何地方都没有引用 ProjectInstaller
(我使用 Shift+F12 检查)。
那么它是如何执行的呢?
编辑
我没有使用installutil。 Program
的 Main()
调用 ManagedInstallerClass.InstallHelper(args);
。
最佳答案
用于安装该服务的程序 (installutil.exe
) 会检查您的程序集并运行安装程序。如其 documentation 中所述:
Installutil.exe uses reflection to inspect the specified assemblies and to find all Installer types that have the System.ComponentModel.RunInstallerAttribute attribute set to true.
The tool then executes either the Installer.Install or the Installer.Uninstall method on each instance of the Installer type. Installutil.exe performs installation in a transactional manner; that is, if one of the assemblies fails to install, it rolls back the installations of all other assemblies. Uninstall is not transactional.
更新:您说您不使用installutil
,而是使用ManagedInstallerClass.InstallHelper
。阅读该类的文档:
This API supports the product infrastructure and is not intended to be used directly from your code.
Handles the functionality of the Installutil.exe (Installer Tool).
所以首先你不应该使用它,其次 - 它与 installutil 的作用相同(因此与这个问题相关 - 使用反射来检查你的程序集并运行安装程序)。我认为 installutil 工具实际上只是调用此方法。请注意,您必须在 ManagedInstallerClass.InstallHelper
的 args
中传递程序集的路径,这样它就知道要通过反射检查哪个程序集并运行安装程序。
关于c# - 什么启动 Windows 服务安装程序的安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49777868/