利用WIX制作安装包(3)

利用WIX安装服务非常简单。只需要短短几句话就可以。当我们创建好一个Windows服务之后。我们在项目中创建一个Service.wxs 文件来安装服务,并且编辑代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
    <Component Id="ProductC" Guid="DE8DD064-C440-4E82-80D7-D05C98753DAF" Directory="PRODUCTC">
      <File Id="ProductCService" Source="$(var.ProductC.TargetDir)ProductC.exe"/>
      <ServiceInstall Id="ProductCServiceInstaller"
                      Type="ownProcess"
                      Name="ProductC"
                      DisplayName="ProductC"
                      Description="ProductC"
                      Start="auto"
                      Account="LocalSystem"
                      ErrorControl="ignore">
        <ServiceConfig DelayedAutoStart="no" OnInstall="yes" />
      </ServiceInstall>
      <ServiceControl Id="ProductCServiceControl"
                      Start="install"
                      Stop="both"
                      Remove="uninstall"
                      Name="ProductC"
                      Wait="yes" />
    </Component>

  </Fragment>
</Wix>

在上述的例子中我们安装了一个名为ProductC 的服务到系统。并且添加了一个ServiceControl 去控制他的行为。然后我们把这个组件关联到产品的某个Feature之后,当产品安装的时候服务就会自定安装。

然而在某些情况用TopShelf实现的服务,我们无法用WIX安装。这个时候我们需要在服务的项目中添加一个ServiceInstall.cs 文件,并添加如下代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;


namespace DitronicsHISvr
{
        [RunInstaller(true)]
        public class HardwareServiceInstaller : Installer
        {
                public ServiceInstaller ServiceInstaller;

                public ServiceProcessInstaller ServiceProcessInstaller;

                public HardwareServiceInstaller()
                {
                        this.InitializeComponent();
                }

                private void InitializeComponent()
                {
                        ServiceInstaller = new ServiceInstaller();
                        ServiceProcessInstaller = new ServiceProcessInstaller();

                        this.ServiceProcessInstaller.Account = ServiceAccount.LocalService;

                        ServiceInstaller.Description = "Service used to interface with Bill Validators.";
                        ServiceInstaller.DisplayName = "Ditronics HI Server";
                        ServiceInstaller.ServiceName = "DitronicsHISvr";

                        this.Installers.AddRange(new Installer[] { this.ServiceProcessInstaller, this.ServiceInstaller });
                }
        }
}

添加上述代码之后,我们就可以正常的使用WIX进行安装服务了。服务上述代码中的ServiceName和Description一定要和WIX中的ServiceName和description一致。不然会出现服务安装好了,无法启动的bug。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值