繁体   English   中英

无法使用WiX启动Windows服务

[英]Can't start Windows service with 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>

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

服务无法启动。 验证您具有启动系统服务的足够特权。

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

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

我在Internet上搜索了此问题,但没有找到任何解决方案。

我如何解决它?

那是我的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" />

我有同样的错误,在我的情况下,我的文件元素中缺少KeyPath='yes' Vital="yes"

这是我的组件定义:

<Component Id="ComponentName"
           Guid="3aa1d5a5-28f0-4753-8e4b-a7ac0848d8be" >
    <File Id='ServiceFile'
          Name='Service.exe'
          DiskId='1'
          Source='bin\Service.exe'
          KeyPath='yes'
          Vital="yes"/>

    <ServiceInstall Id="ServiceInstaller"
                    Type="ownProcess"
                    Name="Service"
                    DisplayName="Service"
                    Description="A Service"
                    Start="auto"
                    ErrorControl="normal"
                    />

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

我一直在寻找答案,终于解决了!

保持与ServiceInstall名称相同的ServiceControl名称。

结果:

<?xml version="1.0" encoding="utf-8"?>
<?define ProductVersion = "1.0.0"?>
<?define ProductUpgradeCode = "{E8DFD614-41F6-4592-AD7A-27EA8A49C82E}"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Product Id="*" UpgradeCode="$(var.ProductUpgradeCode)"
           Name="Eyes Relax"
           Version="$(var.ProductVersion)"
           Manufacturer="Ourdark"
           Language="1033">
    <Package Manufacturer="Ourdark" InstallerVersion="100" Languages="1033" Compressed="yes" />

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

    <Property Id="WHSLogo">1</Property>

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder" Name="PFiles">
        <Directory Id="WHS" Name="Eyes Relax">
          <Component Id="EyesRelax" Guid="{78534F5E-FC72-49E6-AF11-4F2068EA7571}">

            <File Id="RelaxEyes.exe.config"
                  Name="RelaxEyes.exe.config"
                  Source="RelaxEyes\bin\Debug\RelaxEyes.exe.config"
                  Vital="yes"
                  KeyPath="no"
                  DiskId="1"/>

            <File Id="RelaxEyes.exe"
                  Name="RelaxEyes.exe"
                  Source="RelaxEyes\bin\Debug\RelaxEyes.exe"
                  Vital="yes"
                  KeyPath="yes"
                  DiskId="1"/>

            <ServiceInstall
              Id="ServiceInstaller"
              Type="ownProcess"
              Vital="yes"
              Name="Eyes Relax"
              DisplayName="Eyes Relax"
              Description="Eyes Relax"
              Start="auto"
              Account="NT AUTHORITY\LocalService"
              ErrorControl="ignore"
              Interactive="no">
            </ServiceInstall>
            <ServiceControl Id="StartService"
                            Start="install"
                            Stop="both"
                            Remove="uninstall"
                            Name="Eyes Relax"
                            Wait="yes" />
          </Component>
        </Directory>
      </Directory>
    </Directory>

    <Feature Id="ProductFeature" Title="WHSDiskManagement" Level="1">
      <ComponentRef Id="EyesRelax" />
    </Feature>
  </Product>
</Wix>

ServiceInstall的用户名应完全合格:

NT AUTHORITY\\NetworkService

NT AUTHORITY\\LocalService

NT AUTHORITY\\SYSTEM

好吧,大约一年半后,我回到了这个项目。 并尝试重新编译它,然后再次启动该服务。 而且有效!

所做的全部更改是,我将clientaccesspolicy.xml添加到我的服务中,并与我的服务一起运行policyServiceHost(类型为WebServiceHost)。 但我认为这并不重要,因为它与我的应用程序内部有关-与服务启动无关。

因此,我尝试了许多变体,例如:

1)this.serviceProcessInstaller.Username = null;

要么

this.serviceProcessInstaller.Username = @“ NT AUTHORITY \\ SYSTEM”;

2)两个或单个ServiceControl部分。

3)Stop =“ both”

要么

Stop =“卸载”

现在一切正常!!!

我不知道发生了什么 我只是将其留给某种类型的错误或系统的某些奇怪配置,或其他任何不允许我之前自动启动服务的问题。 但是现在一切正常。

换句话说,我不知道我的服务无法自动启动的原因是什么。 这是关于“足够的特权” (请参阅第一篇文章),但即使到现在我也不够清楚。

只有一句话。 如果在卸载服务时使用两个ServiceControl部分,则会出现一个警告窗口(Windows 7),并提示自动关闭应用程序(服务),依此类推。 因此,我只接受并很好地卸载了服务。 但是,如果我只使用一个ServiceControl部分(如第一篇文章中的示例),则不会出现警告窗口。 同样,它与1)和3)点组合没有关系。

我会将此片段用于.wxs文件

<?xml version="1.0" encoding="UTF-8"?>
<?define ProductVersion="1.0.0.0" ?>
<?define UpgradeCode="{YOURGUID}" ?>
<?define Manufacturer="SetupWinService" ?>
<?define ProductName="WcfServiceHost" ?>
<?define SkuName="WcfServiceHost" ?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*"
             Name="$(var.ProductName)"
             Language="1049"
             Version="$(var.ProductVersion)"
             Manufacturer="$(var.Manufacturer)"
             UpgradeCode="$(var.UpgradeCode)">
        <!-- do you really need 200? i'd try at least 301 -->
        <Package InstallerVersion="301"
                 Compressed="yes"
                 Languages="1049"
                 InstallPrivileges="elevated"
                 SummaryCodepage="1251"
                 Platform="x86" />
        <Media Id="1"
               Cabinet="$(var.SkuName).cab"
               EmbedCab="yes" />
        <Directory Id="TARGETDIR"
                   Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="ProductDirectory"
                           Name="$(var.ProductName)" />
            </Directory>
        </Directory>
        <ComponentGroup Id="MainComponentGroup">
            <Component Directory="ProductDirectory">
                <File Name="$(var.**Project**.TargetFileName)"
                      Source="$(var.**Project**.TargetPath)"
                      KeyPath="yes"
                      Vital="yes" />
                <ServiceInstall Id="SeviceInstall"
                                Name="$(var.ProductName)"
                                DisplayName="$(var.ProductName)"
                                Type="ownProcess"
                                Interactive="no"
                                Start="auto"
                                Vital="yes"
                                ErrorControl="normal"
                                Account="LOCALSYSTEM">
                </ServiceInstall>
                <ServiceControl Id="ServiceControl_Start"
                                Name="$(var.ProductName)"
                                Start="install"
                                Wait="no" />
                <ServiceControl Id="ServiceControl_Stop"
                                Name="$(var.ProductName)"
                                Stop="both"
                                Remove="uninstall"
                                Wait="yes" />
            </Component>
            <Component Directory="ProductDirectory">
                <File Name="App.config"
                      Source="$(var.**Project**.TargetDir)\app.config"
                      Vital="yes" />
            </Component>
        </ComponentGroup>
        <Feature Id="MainFeature"
                 Level="1">
            <ComponentGroupRef Id="MainComponentGroup" />
        </Feature>
        <!-- added automatic upgrading -->
        <Upgrade Id="$(var.UpgradeCode)">
            <UpgradeVersion Property="UPGRADEFOUND"
                            Minimum="0.0.0.1" IncludeMinimum="yes"
                            Maximum="$(var.ProductVersion)" IncludeMaximum="yes"
                            OnlyDetect="no"
                            IgnoreRemoveFailure="yes"
                            MigrateFeatures="yes"/>
        </Upgrade>
        <InstallExecuteSequence>
            <InstallExecute Before="RemoveExistingProducts" />
            <RemoveExistingProducts Before="InstallFinalize" />
        </InstallExecuteSequence>
    </Product>
</Wix>

有了这个基本的System.ServiceProcess.ServiceBase实现(与您的并没有什么不同)

public partial class Service : ServiceBase
{
    public Service()
    {
        this.InitializeComponent();
    }

    public static void Main()
    {
        Run(new Service());
    }

    #region Service Commands

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }

    protected override void OnPause()
    {
        this.OnStop();
    }

    #endregion
}

有了这个片段,我得到了一个演示项目来工作...

可以正常运行的演示项目 -如果仍然失败,请修改代码,以便我重现您的问题!

我在某些计算机上遇到此错误。 同一可执行文件可在某些文件上使用,而在其他文件上则会出现此错误。

在这些计算机上更新.NET 1.1 / 2.0 / 3.0会有帮助(在Windows XP,7和8.1上对我有用)。

暂无
暂无

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

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