WiX MSI 安装 Windows 服务而不启动它

WiX MSI install Windows service without starting it

我正在尝试为我的 Windows 服务创建一个 WiX MSI 安装程序,它将安装该服务,但不会启动它。我似乎找不到任何地方解释如何执行此操作或是否可能。

我尝试删除用于启动服务的 ServiceControl 以及切换 ServiceInstall 上的 Start 属性,但没有成功。必须有可能做到这一点,对吧?我只想要 MSI 文件来安装服务并让用户在需要时启动它。

<Component Id="ServiceInstaller" Guid="9e578e3d-0339-425c-8633-f54ffaaa4921">

    <ServiceInstall Id="ReportingServiceInstaller"
                    Type="ownProcess"
                    Vital="yes"
                    Name="WindowsService.exe"
                    DisplayName="WindowsService"
                    Description="Wickedly awesome and amazing service."
                    ErrorControl="ignore"
                    Account="NT AUTHORITY\LocalService"
                    Start="auto"
                    Interactive="no" />

    <ServiceControl Id="ServiceControl_Stop"
                    Name="WindowsService.exe"
                    Stop="both"
                    Remove="uninstall"
                    Wait="no" />

</Component>

不要使用 ServiceControl 元素,因为它将启动和停止服务。通过调用它,您要求安装程序对服务执行某些操作。您只需要调用 ServiceInstall 即可创建服务。正如 Stefan Wanitzek 所建议的,您应该在 ServiceInstall 的 Start 属性中使用需求。这是为了让您的服务在用户重新启动计算机时不会启动 运行。如果您的安装程序也正在安装 .exe,则还要使用安装服务添加该文件。我建议使用从您的代码中获取的以下内容:

<Component Id="ServiceInstaller" Guid="3e412e3d-0339-325c-8633-f54ffaaa4921">
        <File Id="WindowsService.exe" 
         Name="WindowsService.exe" 
         KeyPath="yes"
         Source="Path to the EXE"/>
        <ServiceInstall Id="ReportingServiceInstaller"
                Type="ownProcess"
                Vital="yes"
                Name="WindowsService"                    
                DisplayName="WindowsService"
                Description="Wickedly awesome and amazing service."
                ErrorControl="ignore"
                Account="NT AUTHORITY\LocalService"
                Start="demand"
                Interactive="no" />
</Component>

devfunkd的解决方案是正确的,但是有几个问题需要先讨论一下。

一,可以去掉ServiceControl,也可以只去掉Start属性。这有助于在更新时停止服务。不,Start 没有 "none" 选项,编写 Start="" 也是无效的,这将允许使用一个简单的变量而不是复制粘贴整个组件。

第二,每当更新服务时,RestartManager 都会参与进来。如果服务是运行,服务被停止,然后Setup按照指令去做,包括不启动服务,然后RestartManager恢复服务的原始状态(已启动)。我花了一天时间弄明白了。

请注意,您可以禁用安装程序与 RestartManager 的交互 (请参阅 https://docs.microsoft.com/en-us/windows/desktop/Msi/msirestartmanagercontrol),但是如果安装程序因正在使用的文件而出错,您需要确保不需要重新启动。

换句话说,无论是完全删除 ServiceControl 还是只删除 Start 属性,为了确保服务在更新后不启动,您需要在安装前手动停止它们。