How to get MSI Installer to run some code on uninstall of a service
Asked Answered
E

2

1

I'm having troubles with some could I would like to execute when the service will be uninstalled. I've added the code to both the System.ServiceProcess.ServiceProcessInstaller BeforeUninstall event, and
to the override method OnBeforeUninstall, but that did not work too.

When using my InstallShield msi to uninstall the service this code does not get executed.

How can I force the service to launch some code during uninstall? Do I need to use a different event in my C# service? Do I need to change something in my MSI?

Note: My problem is identical to the following https://community.flexerasoftware.com/showthread.php?149176-MSI-Uninstall-NET-Service-does-not-launch-BeforeUninstall-event

Thanks, sagar

Engen answered 2/5, 2018 at 9:46 Comment(0)
W
1

You'll need to be more explicit about everything you've done. For example there is no automatic calling of installer class methods unless they are explicitly added to your setup as custom actions (in your case an uninstall custom action). You should also explain exactly how the service was installed.

Note that installer classes were invented by Visual Studio setups, and there is typically no need to use them because tools like InstallShield usually support the standard ServiceInstall and ServiceControl functionality in Windows Installer. Installer classes are unnecessary. Also, those events are Visual Studio specials, and it's not clear to me if InstallShield supports them. If you are using an Uninstall method to uninstall a service then just add your "before" code to before the Base.Uninstall() call.

If you want code to run when the service is uninstalled, the more usual way is to have a custom action that calls your code, and condition the call on REMOVE="ALL" (for an uninstall) or other conditions depending on your exact requirement. For example, if you do an upgrade MSI to upgrade your existing product do you still want to run that uninstall code?

Williwaw answered 2/5, 2018 at 18:3 Comment(1)
I use the service install and service control tables to install and start the service. I could use a custom action but that would involve duplication of the code in the windows service. I am trying to call a web service method to unregister the application on uninstall. I dont want to call that code on upgrade.Engen
V
0

.NET Installer Class: PhilDW has already said it all, but I just want to add a couple of things. I assume you are using System.Configuration.Install.Installer and the ServiceProcessInstaller Class (there is a sample in the Installshield help file). There used to be (and I think there still is) a flag for each Installshield component called .NET Installer Class. You can set this flag to Yes, and then I believe Installshield itself will take care of running your installer class methods (Install, Commit, Rollback, Uninstall - whichever one is implemented I think, but I have never tested all scenarios. I don't use this feature).

InstallUtil.exe: Running InstallUtil.exe would indeed call these methods. I am not sure how Installshield calls the methods under the hood (during setup execution) - they may actually be calling InstallUtil.exe for all I know, or some more low-level API call.

Conclusion: Just go to the components view and click on the component containing your service.exe file - and set the flag mentioned above. The exe must be a key-file for the component in question - and it must be a .NET assembly. Like PhilDW wrote, I wouldn't use these methods since they are intended to be used during service development only - not during deployment. For one thing you could run into runtime errors when the methods try to run faulting your whole setup (and often unnecessarily so), and there are also likely issues with rollback and potentially other problems. Can we ask what the methods are actually doing? Perhaps you are just deleting some log files? There are other ways to do that - most significantly using the RemoveFile table.

Vostok answered 2/5, 2018 at 22:6 Comment(2)
I am trying to call a web service method to unregister the application on uninstall. It is possible for a user to uninstall the application and forgetting to unregister it on on the server. I can have exception handlers for the methods which will take care of failure scenariosEngen
My apologies for saying Installshield. I actually meant the VS installer wizard. I could not figure out where the component view is. In orca, there is a component table, with columns attribute and condition. Is this what you mean? or is it something in the installer wizard project?Engen

© 2022 - 2024 — McMap. All rights reserved.