I was using OctopusDeploy to install a Rebus service endpoint as a Windows Service from a console application created using Topshelf.
Octopus has built-in support for Windows Services that uses sc.exe under the hood. I'd used this many times previously without hitch, but for some reason the install seemed to start before the service was fully removed. As Topshelf can do all the service set up, I decided to try using that instead.
My deployment step in Octopus was configured to use the standalone PowerShell scripts. Installing using Topshelf is simple - just call the executable using the commandline install --autostart. Removing a Topshelf service is just as simple - call the executable using the uninstall commandline.
The only hard part is finding the executable path for the service so that you can remove the service before re-deploying. PowerShell doesn't have built-in support for this at time of writing, but you can do that pretty easily using WMI. The following function is based on David Martin's SO answer and finds the executable path from the service name:
From that point, it's pretty easy to remove the service using Topshelf:
An alternative approach would be to use a known installation location each time and derive the executable path using Octopus' powerful variables.
The code for the pre-deployment and post-deployment tasks can be found in this gist.
The full Topshelf command line reference is here.