How to configure a Windows Service from a HarvestDirectory in Wix4?

32 Views Asked by At

I have configured a project harvesting component inside my Setup.wixproj and have referenced that component inside my Components.wxs. Inside the harvested component, there is a file called MyService.exe.

Now, how do I configure a Windows Service for that file from inside the Components.wxs? Or, in other words, how do I create additional configurations for files from inside the harvested component?

I couldn't understand the concept behind it.

I was able to do it by cloning the file with a new name and then configuring the Windows Service separately, like this code below, but this isn't the right way to do it, right?

<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension"
     xmlns:netfx="http://wixtoolset.org/schemas/v4/wxs/netfx">
  <Fragment>
    <ComponentGroup Id="MainComponent" Directory="INSTALLDIR">

      <!-- Collected from HarvestDirectory element inside Setup.wixproj -->
      <ComponentGroupRef Id="HarvestedBinaries" />

      <Component Id="WindowsServiceComponent" Guid="F11E31E4-46B6-46D9-8FD2-52E8B81B27D1" Directory="INSTALLDIR">

        <!-- The only solution I have found is to create a copy of the file from HarvestedBinaries. -->
        <File Source="MyService.exe" Name="MyCopiedService.exe" KeyPath="yes" />

        <ServiceInstall
            Id="ServiceInstaller"
            Type="ownProcess"
            Vital="yes"
            Name="MyService"
            DisplayName="My Service"
            Start="auto"
            ErrorControl="normal"
            Account="LocalSystem"
            />
        <ServiceControl
            Id="ServiceControl"
            Name="MyService"
            Start="install"
            Stop="both"
            Remove="uninstall"
            Wait="no"
            />
      </Component>

    </ComponentGroup>  
  </Fragment>
</Wix>

This is how I configured the harvesting component:

<Project Sdk="WixToolset.Sdk/4.0.2">
  <PropertyGroup>
    <OutputName>MyService</OutputName>
    <NetVersion>net7.0</NetVersion>
    <WixUseInternalCompressedCabinet>true</WixUseInternalCompressedCabinet>
    <!-- ... -->
    
    <!-- Custom properties -->
    <MyServiceProject>..\..\src\MyService\MyService.csproj</MyServiceProject>
    <MyServicePublishedFolder>..\..\src\MyService\bin\$(Configuration)\$(NetVersion)\publish</MyServicePublishedFolder>
    
  </PropertyGroup>
  <!-- ... -->

  <ItemGroup>
    <!-- Collecting published files from the main project. -->
    <BindPath Include="$(MyServicePublishedFolder)" />
    <HarvestDirectory Include="$(MyServicePublishedFolder)">
      <ComponentGroupName>HarvestedBinaries</ComponentGroupName>
      <DirectoryRefId>INSTALLDIR</DirectoryRefId>
      <SuppressRootDirectory>true</SuppressRootDirectory>
      <SuppressRegistry>true</SuppressRegistry>
      <KeepEmptyDirectories>true</KeepEmptyDirectories>
    </HarvestDirectory>
  </ItemGroup>

</Project>
0

There are 0 best solutions below