Undoing WiX Custom Actions During Uninstall
In my previous post about creating Custom Actions with WiX, I showed how to create a custom action project and invoke the custom action from a setup project. In my particular case, the custom action was making several edits to an existing web.config file to incorporate a new HTTP module that was being installed. Since these edits to the web.config are not automatically removed during uninstall of the HTTP module, I need a compensating custom action to perform this task.
Since I already have a custom action project and the associated CustomActions class from the previous post, the easiest thing to do is to incorporate the custom action for the uninstall into the same class (Remember, a custom action is represented as a static method annotated with the CustomAction attribute that ultimately gets generated into a DLL entry point that is callable by the MSI engine). So to add the new custom action, I simply need to add another method to my CustomActions class. The original custom action to make the web.config edits and the new custom action to remove the edits on uninstall are shown below.
As with the original custom action, I need a reference to the new custom action in the *.wxs file. The original Binary element and CustomAction element are shown along with the new CustomAction element (lines 8-12) to remove the configuration changes.
The only differences between the two CustomAction elements are the Id and DllEntry attributes, where the DllEntry attribute indicates the CustomAction method to invoke.
Up to this point, the process of adding the compensating CustomAction is exactly the same as adding the original CustomAction. However, I only want the new custom action to run during uninstall of the HTTP module. I can control that with a condition on the custom action’s entry in the InstallExecuteSequence.
The entry for the new custom action is shown on lines 6-8 above. Line 7 provides the condition when the custom action should be run. In this case it references the Installed and UPGRADINGPRODUCTCODE properties, indicating that the product should already be installed and an upgrade to the product is not being performed (i.e. during uninstall).
You’ll also notice the condition NOT Installed added to the original custom action performing the configuration edits. That’s because I want it to only run during install, not during uninstall, otherwise I’ll get my edits added back into the web.config file.