简体   繁体   English

在自定义操作中安装Windows服务

[英]install windows service in custom actions

I have created ac# 4.0 windows service. 我创建了ac#4.0 windows服务。 I have created an installer project (.msi) for it which installs the service to a particular folder. 我为它创建了一个安装程序项目(.msi),它将服务安装到特定文件夹。

To automate the process fully, I would like to install the service as part of the custom actions I have for my installer. 为了完全自动化该过程,我想将该服务作为我对安装程序的自定义操作的一部分进行安装。

How can I code my custom actions to install or when uninstalling the msi, uninstall the windows service? 如何编写自定义操作以进行安装或卸载msi时,卸载Windows服务?

You can use the ServiceInstaller class. 您可以使用ServiceInstaller类。 A quick solution would be to find installutil tool and execute it against your Service.exe, but you have to capture the output to see whether the installation succeeded or not and you don't have much control over Install , Commit , Rollback , and Uninstall phases. 一个快速的解决方案是找到installutil工具并针对您的Service.exe执行它,但您必须捕获输出以查看安装是否成功,并且您无法控制InstallCommitRollbackUninstall阶段。

Simple answer: don't. 简单回答:不要。 The proper way is to install it using the MSI database itself, ie ServiceInstall and ServiceControl tables. 正确的方法是使用MSI数据库本身安装它,即ServiceInstallServiceControl表。 Every single "convenient" IDE for MSI creation and also WiX come with primitives to make use of this builtin facility. 用于MSI创建的每个“方便”IDE以及WiX都带有原语以利用这种内置工具。

The very reason that this is best practice , just like including the COM registration in your MSI instead of calling DllRegisterServer of the COM (DLL) to register is that your application may be defunct at the time the user attempts to remove it. 这是最佳实践的原因,就像在MSI中包含COM注册而不是调用COM(DLL)的DllRegisterServer来注册一样,您的应用程序可能在用户尝试删除它时失效。

The database actions can still be executed even by a newer Windows Installer, say after an upgrade of Windows itself, while your code may refuse to run or may not run for other reasons. 即使是较新的Windows安装程序仍然可以执行数据库操作,例如在Windows自身升级之后,您的代码可能会拒绝运行或由于其他原因而无法运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM