Wix Custom Action only on uninstall

wix

I am having a problem getting a Custom action to only run when I am uninstalling my service using Wix.

<CustomAction Id='InstallService'
              FileKey='Service.exe'
              ExeCommand='install'
              Execute='immediate'
              Return='check'
              Impersonate='yes'>NOT Installed</CustomAction>

<CustomAction Id='UninstallService'
              FileKey='Service.exe'
              ExeCommand='uninstall'
              Execute='immediate'
              Return='check'
              Impersonate='yes'>Installed AND NOT REINSTALL</CustomAction>

<InstallExecuteSequence>
  <Custom Action='UninstallService' After='StopServices'/>
  <Custom Action='InstallService' Before='StartServices'/>
</InstallExecuteSequence>

this is the component …

  <Component Id="ProductComponent">
    <File Id="MyService.exe"
          Name="MyService.exe"
          Source="..\MyService\bin\Release\MyService.exe"
          Vital="yes"
          KeyPath="yes"
          DiskId="1"/>

    ...

    <ServiceControl Id='ServiceControl'
                    Name='MyService'
                    Start='install'
                    Stop='both'/>
  </Component>

When I run the installer I get an error. Looking in the event log I find this …

Product: MyService — Error 1721. There is a problem with this Windows Installer package. A program required for this install to complete could not be run. Contact your support personnel or package vendor. Action: UninstallService, location: C:\Program Files (x86)\MyService\MyService.exe, command: uninstall

I have also tried this …

<CustomAction Id='UninstallService'
              FileKey='Service.exe'
              ExeCommand='uninstall'
              Execute='immediate'
              Return='check'
              Impersonate='yes'>Installed AND NOT UPGRADINGPRODUCTCODE</CustomAction>

Note: I am using custom actions to install/uninstall the service because I've made use of TopShelf.NET

Best Answer

Your best bet is to tie the action of the custom action to the action state of the Component.

<InstallExecuteSequence>
   <Custom Action="UninstallService">$ProductComponent=2</Custom>
   <Custom Action="InstallService">$ProductComponent=3</Custom>
</InstallExecuteSequence>

Also, you're going to need your CustomAction elements to be Execute='deferred'.

Also, text in the CustomAction element is only allowed if you're creating a script custom action. It doesn't seem like that's what you are doing.

Adding custom actions requires quite a bit of understanding. It is unfortunate that a 3rd party platform would force you to use custom actions.

Related Topic