使用wix启动Windows服务的问题

时间:2014-08-27 17:13:44

标签: windows service wix

我有一个需要安装并使用wix(Windows xml Installer)启动的服务。

我做了小窗口服务只是为了测试目的。 但无法安装和启动wix安装项目。

以下是设置的代码

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
  xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Product Id="*" Name="WindoServiceSetup" Language="1033" Version="1.0.0.0" Manufacturer="Microsoft" UpgradeCode="26b8eb77-ad9e-4661-9559-3480f3e21434">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate />
    <Feature Id="ProductFeature" Title="WindoServiceSetup" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>
  </Product>
  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="WindoServiceSetup" />
      </Directory>
    </Directory>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="FileInstalled" Guid="9FED4055-8C10-49CA-A96D-532C44AAD415">
        <File Id="In" Name="WinService" Source="E:\WinService\WinService\WinService\bin\Debug\WinService.exe" KeyPath="yes"></File>
      </Component>
        <Component Id="FileInstalled4" Guid="3890C9A0-C5AA-49B7-AC78-60065CD2390E">
        <File Id="In1" Name="WinService1" Source="E:\WinService\WinService\WinService\bin\Debug\WinService.pdb" KeyPath="yes"></File>
        </Component>
      <Component Id="FileInstalled1" Guid="57A5E2AC-B050-4830-9FBD-180D3B2B1529">
        <File Id="In2" Name="WinService2" Source="E:\WinService\WinService\WinService\bin\Debug\WinService.InstallLog" KeyPath="yes"></File>
      </Component>
      <Component Id="FileInstalled2" Guid="1866F830-C90B-4733-B987-437AF376D9FA">
      <File Id="In3" Name="WinService4" Source="E:\WinService\WinService\WinService\bin\Debug\WinService.exe.config" KeyPath="yes"></File>
      </Component>
      <Component Id="FileInstalled3" Guid="5306EDB1-7FD5-4070-9C98-FB38CCEB68EC">
        <File Id="In4" Name="WinService3" Source="E:\WinService\WinService\WinService\bin\Debug\InstallUtil.InstallLog" KeyPath="yes"></File>
      </Component>
      <Component Id="SeviceSetup" Guid="672FD8AD-7BD9-4924-9059-7FC7042DC718" KeyPath="yes">
        <ServiceInstall Id="Installs"  Name="WinService.exe" Start="auto"  ErrorControl="ignore" Interactive="no" DisplayName="Testservice" Type ="ownProcess" Account="LocalSystem">
        </ServiceInstall>
        <ServiceControl Id="InsatllControl" Name="ServiceCnt" Start="both" Wait="no">
        </ServiceControl>
        <ServiceControl Id="InstallCntrolstop" Name="Servicercnt1" Stop="uninstall" Remove="uninstall" Wait="no">
        </ServiceControl>
      </Component>
      <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
      <!-- <Component Id="ProductComponent"> -->
      <!-- TODO: Insert files, registry keys, and other resources here. -->
      <!-- </Component> -->
    </ComponentGroup>
  </Fragment>
</Wix>

和Windows服务代码

class WinService : System.ServiceProcess.ServiceBase
    {
        // The main entry point for the process
        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun =
              new System.ServiceProcess.ServiceBase[] { new WinService() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ServiceName = "WinService";
        }
        private string folderPath = @"D:\sam";
        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            if (!System.IO.Directory.Exists(folderPath))
                System.IO.Directory.CreateDirectory(folderPath);

            FileStream fs = new FileStream(folderPath + "\\WindowsService.txt",
                                FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" WindowsService: Service Started at " +
               DateTime.Now.ToShortDateString() + " " +
               DateTime.Now.ToShortTimeString() + "\n");
            m_streamWriter.Flush();
            m_streamWriter.Close();
        }
        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            FileStream fs = new FileStream(folderPath +
              "\\WindowsService.txt",
              FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" WindowsService: Service Stopped at " +
              DateTime.Now.ToShortDateString() + " " +
              DateTime.Now.ToShortTimeString() + "\n");
            m_streamWriter.Flush();
            m_streamWriter.Close();
        }
    }

问题 验证您是否已指定运行系统服务的权限。

正如我一直在解释的那样,人们说这个信息对于所有问题都很常见 我有管理员权限,还添加了所有依赖文件仍然没有运气... 任何帮助..

1 个答案:

答案 0 :(得分:0)

您的名称定义在ServiceInstall标记内错误。在这里,您必须指定服务的名称,而不是可执行文件的路径。然后您错过了ServiceInstall标记之前的File定义,您可以在其中指定服务可执行文件的路径。以我的经验,最好将单独的ComponentGroup用于ServiceInstall。这是一个工作示例:

<Fragment>
    <ComponentGroup Id="CG_ServiceInstaller" Directory="INSTALLFOLDER">
      <Component Guid="F7E041AB-3227-4272-92C1-4A12685F30AF" >
        <File Source="$(var.YourService.TargetDir)YourService.exe" KeyPath="yes"/>
        <ServiceInstall Id="ServiceInstaller"
                          Type="ownProcess"
                          Name="Your Service Name"
                          DisplayName="TCP Json Service"
                          Description="TCP Server on port 1234"
                          Start="auto"
                          ErrorControl="normal"
                          Account="LocalSystem"/>
        <ServiceControl Id="StartService" Start="install" Stop="both" Remove="uninstall" Name="YourService" Wait="yes" />
      </Component>
    </ComponentGroup>
  </Fragment>