windows - 无法使用 WiX 启动 Windows 服务

标签 windows service wix

我有以下 WiX 项目来安装我的服务:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="GUID" Name="SetupWinService" Language="1049"
           Version="1.0.0.0" Manufacturer="SetupWinService"
           UpgradeCode="GUID">
    <Package InstallerVersion="200" Compressed="yes"
             Languages="1049" SummaryCodepage="1251"
             InstallPrivileges="elevated"/>

    <Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="WinService" Name="My Windows Service">
        </Directory>
      </Directory>
    </Directory>

    <DirectoryRef Id="WinService">
      <Component Id="WinServiceInstallation" Guid="GUID">
        <File Id="ClientService.exe"
              Name="ClientService.exe"
              Source="...\ClientService.exe"
              Vital="yes" KeyPath="yes" DiskId="1"/>
        <File Id="App.config"
              Name="App.config"
              Source="...\App.config"
              Vital="yes" KeyPath="no" DiskId="1"/>

            <!--And some DLLs here-->

        <ServiceInstall Id="ServiceInstaller"
                        Type="ownProcess"
                        Vital="yes"
                        Name="WcfServiceHost"
                        DisplayName="WcfServiceHost"
                        Description="Hosts Wcf Service"
                        Start="auto"
                        Account="LocalSystem"
                        ErrorControl="ignore"
                        Interactive="no">
        </ServiceInstall>
        <ServiceControl Id="StartService" Name="WcfServiceHost"
                        Start="install" Stop="uninstall" Remove="uninstall"
                        Wait="yes" />
      </Component>
    </DirectoryRef>

    <Feature Id="Complete" Title="SetupWinService" Level="1">
      <ComponentRef Id="WinServiceInstallation" />
      <ComponentGroupRef Id="Product.Generated" />
    </Feature>
  </Product>
</Wix>

我可以安装我的服务,但安装后无法启动。它告诉:

Service failed to start. Verify that you have sufficient privileges to start system services.

但我以管理员身份运行我的安装程序 (Windows 7 Professional) 并且还禁用了 UAC .此外,我可以通过命令提示符使用 instalutil.exe 安装和运行服务(我的服务项目包括 Installer 类的实现,通常根据 this article 进行标记),在这种情况下,所有服务都可以正常工作。

如果我将 ServiceControl 元素的 Wait="yes"替换为 "no",则服务安装没有错误,但它不会启动。在这种情况下,我也无法手动启动该服务,因为该服务启动并立即停止并显示消息“本地计算机上的服务已启动然后停止。某些服务如果没有工作可做,则会自动停止”。

我在互联网上搜索过这个问题,但没有找到任何解决方案。

我该如何解决?

这是我的 Installer 类的代码:

[RunInstaller(true)]
public class ProjectInstaller : Installer
{
    private ServiceProcessInstaller serviceProcessInstaller;
    private ServiceInstaller serviceInstaller;

    public ProjectInstaller()
    {
        this.serviceProcessInstaller = new ServiceProcessInstaller();
        this.serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
        this.serviceProcessInstaller.Username = null;
        this.serviceProcessInstaller.Password = null;
        this.serviceInstaller = new ServiceInstaller();
        this.serviceInstaller.ServiceName = "ClientServicesHost";
        this.serviceInstaller.StartType = ServiceStartMode.Automatic;
        this.Installers.Add(serviceProcessInstaller);
        this.Installers.Add(serviceInstaller);
        this.AfterInstall +=
                new InstallEventHandler(ProjectInstaller_AfterInstall);
    }

    void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
    {
        ServiceController sc = new ServiceController("ClientServicesHost");
        sc.Start();
    }
}

还有我的 Windows 服务:

class WindowsClientService : ServiceBase
{
    public ServiceHost serviceHost = null;

    public WindowsClientService()
    {
        this.ServiceName = "WcfServiceHost";
    }

    public static void Main()
    {
        ServiceBase.Run(new WindowsClientService());
    }

    protected override void OnStart(string[] args)
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
        }

        // Create a ServiceHost for WcfClientService type
        // and provide the base address.
        serviceHost = new ServiceHost(typeof(WcfClientService));

        // Open the ServiceHost to create listeners
        // and start listening for messages.
        serviceHost.Open();
    }

    protected override void OnStop()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }
}

有人指出我的服务自动停止的原因——启动后什么都不做。是真的吗?我的服务创建监听器并开始监听 - 这是“什么都不做”吗?

最佳答案

我在使用 WiX 3.7.821.0 和我的服务时遇到了同样的问题。安装了一段时间,同样烦人的“服务启动失败。请确认您有足够的权限启动系统服务”。

我尝试了很多,但最后还是为 <ServiceControl> 使用了两个部分而不是试图将所有内容都塞进一个。一个用于开始,一个用于停止。现在服务开始正常了。

这不起作用:

<ServiceControl Id="StartService" 
                Start="install" 
                Stop="both" 
                Remove="uninstall" 
                Name="MyService" 
                Wait="yes" />

这个有效:

<ServiceControl Id="ServiceControl_Start"
                Name="MyService"
                Start="install"
                Wait="no" />
<ServiceControl Id="ServiceControl_Stop"
                Name="MyService"
                Stop="both"
                Remove="uninstall"
                Wait="yes" />

关于windows - 无法使用 WiX 启动 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12370634/

相关文章:

exception - 使用View-Controller-Service体系结构在Grails中运行时错误处理

WIX 错误 - ICE03 : Not a valid foreign key

Windows 任务计划程序将只运行一次应用程序

.net - 如何使用基于WiX的安装程序为.NET应用程序创建性能计数器?

Wix CloseApplication 元素无法与 Bundle.wxs 一起使用

linux - 连接到托管在 CentOS 上的 MongoDb 服务器失败

windows - 在 Windows Batch 中打印文本文件的特定行

java - java中如何从系统中获取正在运行的jar文件名

php - 如何在适用于 Windows 的 XAMPP v3.2.1 上使用 Eclipse 配置 PHP XDebug?

windows - 应用 Windows 10 Creators Update 后 CouchDB 无法启动