amateur
amateur

Reputation: 44605

install windows service in custom actions

I have created a c# 4.0 windows service. I have created an installer project (.msi) for it which installs the service to a particular folder.

To automate the process fully, I would like to install the service as part of the custom actions I have for my installer.

How can I code my custom actions to install or when uninstalling the msi, uninstall the windows service?

Upvotes: 2

Views: 3198

Answers (2)

0xC0000022L
0xC0000022L

Reputation: 21279

Simple answer: don't. The proper way is to install it using the MSI database itself, i.e. ServiceInstall and ServiceControl tables. Every single "convenient" IDE for MSI creation and also WiX come with primitives to make use of this builtin facility.

The very reason that this is best practice, just like including the COM registration in your MSI instead of calling DllRegisterServer of the COM (DLL) to register is that your application may be defunct at the time the user attempts to remove it.

The database actions can still be executed even by a newer Windows Installer, say after an upgrade of Windows itself, while your code may refuse to run or may not run for other reasons.

Upvotes: 2

dan radu
dan radu

Reputation: 2782

You can use the ServiceInstaller class. A quick solution would be to find installutil tool and execute it against your Service.exe, but you have to capture the output to see whether the installation succeeded or not and you don't have much control over Install, Commit, Rollback, and Uninstall phases.

Upvotes: 2

Related Questions