Using WiX to install the service is simple. It takes just a few words to get there. After we create the good one Windows service. We create a file in the project Service.wxs
to install the service, and edit the code as follows:
<?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< /span>= "ownprocess" name = "PRODUCTC" displayname =" PRODUCTC " description =" Pr ODUCTC " 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>
In the example above we have installed a ProductC
service-to-system named. And added a ServiceControl
way to control his behavior. Then we associate this component to a feature of the product and the service will be installed when the product is installed.
However, in some cases services implemented with Topshelf, we cannot install with WiX. At this point we need to add a ServiceInstall.cs file to the service's project and add the following code.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Configuration.Install;usingSystem.Linq;usingSystem.ServiceProcess;usingSystem.Text;usingSystem.threading.tasks;namespace ditronicshisvr{[Runinstaller (true)] Public classHardwareserviceinstaller:installer { PublicServiceInstaller ServiceInstaller; PublicServiceProcessInstaller ServiceProcessInstaller; Public Hardwareserviceinstaller() { This. InitializeComponent (); }Private void InitializeComponent() {ServiceInstaller =NewServiceInstaller (); ServiceProcessInstaller =NewServiceProcessInstaller (); 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 (NewInstaller[] { This. ServiceProcessInstaller, This. ServiceInstaller}); } }}
After adding the above code, we can use WiX to install the service properly. Service ServiceName and description in the above code must be consistent with the ServiceName and description in WiX. Otherwise there will be a bug that the service is installed and cannot be started.
Making installation packages with WiX (3)