无法使用 WiX 启动 Windows 服务 [英] Can't start Windows service with WiX

查看:69
本文介绍了无法使用 WiX 启动 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following WiX project to install my service:

<?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>

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

I can install my service, but I can't start it after installing. It tells:

服务启动失败.确认您有足够的权限来启动系统服务.

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

但我以管理员身份运行安装程序 (Windows 7 Professional) 并禁用 UAC.此外,我可以通过命令提示符使用 instalutil.exe 安装和运行该服务(我的服务项目包括 Installer 类的实现,一般根据 这篇文章),在这种情况下,该服务一切正常.

But I run my installer as administrator (Windows 7 Professional) and also disable UAC. Furthermore, I can install and run the service with instalutil.exe through command prompt (my service project includes realization of Installer class and in general is marked up according to this article), and all works fine with the service in that case.

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

If I replace Wait="yes" of the ServiceControl element to "no", the service installs without errors, but it does not start. I also can't start the service manually in that case, because the service starts and immediately stops with message "service on Local Computer started and then stopped. Some services stop automatically if they have no work to do".

我在网上搜索了这个问题,但没有找到任何解决方案.

I searched about this problem on the Internet, but I didn't find any solutions.

我该如何解决?

这是我的安装程序类的代码:

That is the code of my Installer class:

[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 服务:

And my Windows service:

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;
        }
    }
}

有人指出我的服务自动停止的原因 - 启动后它什么也不做.是真的吗?我的服务创建侦听器并开始侦听 - 是什么都不做"吗?

I was pointed out that the reason of my service automatically stops - it does nothing after start. Can it be? My service creates listeners and starts listening - is that "does nothing"?

推荐答案

嗯,大约一年半后我又回到了这个项目.并尝试重新编译它并再次启动此服务.它有效!

Well, I returned to this project after about 1 and half year. And trying to recompile it and start this service again. And it works!

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

All that changed is I added clientaccesspolicy.xml to my service and run policyServiceHost (of type WebServiceHost) along with my service. But I don't think it is important because of it relates to insides of my application - not to service start.

所以我尝试了很多变体,比如:

So I tried many variations, like:

1) this.serviceProcessInstaller.Username = null;

1) this.serviceProcessInstaller.Username = null;

this.serviceProcessInstaller.Username = @"NT AUTHORITY\SYSTEM";

this.serviceProcessInstaller.Username = @"NT AUTHORITY\SYSTEM";

2) 两个或单个 ServiceControl 部分.

2) Two or single ServiceControl sections.

3) 停止="两者"

停止="卸载"

现在一切正常!!!

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

I don't know what is happening. I just leave it to some type of bug or something strange configuration of my system or whatever else that doesn't allow me to start my service automatically before. But now all worksfine.

换句话说,我没有找到我的服务不会自动启动的原因.这是关于足够的特权"(请参阅第一篇文章)但即使现在对我来说还不够清楚.

Another words, I didn't find out what was the reason of my service won't to start automatically. It was about "sufficient privileges" (see the first post) but it is not clear enough for me even now.

只有一句话.如果我在卸载服务时使用两个 ServiceControl 部分,则会出现一个警告窗口(Windows 7)并提供自动关闭应用程序(服务)等.所以我只是接受并且服务卸载得很好.但是如果我只使用一个 ServiceControl 部分,则不会出现警告窗口,就像我在第一篇文章中的示例一样.再次与 1) 和 3) 点组合没有关系.

Only one remark. If I use two ServiceControl sections while uninstalling the service, a warning window appears (Windows 7) and offer to close application (service) automatically and so on. So I just accept and service uninstalls well. But no warning windows appear if I use only one ServiceControl section as in my example in the first post. And again it is no relations to 1) and 3) points combination.

这篇关于无法使用 WiX 启动 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆