利用WIX制作安装包(3)

原文 利用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。

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/9054560.html