Restarting Windows service and conditional launch

Jun 16, 2016 at 3:23 PM
I have a Windows service which I install and start using WixSharp. This works fine. The service is extensible in that additional functionality can be registered with it through another WixSharp installer (it drops a config file and an assembly for the service to pick up). I need some guidance on two questions:
  1. Following the second install, how would I restart the running service so the additional functionality can be picked up.
  2. What would be the recommended approach for a launch condition the makes sure the Windows service has been previously installed?
Thanks,

Jim
Jun 19, 2016 at 2:34 AM
As far as I know neither MSI nor WiX offer functionality for controlling external services (not the ones being installed). Though I can be wrong.

However Wix# allows easy control of any OS component including services. Have a look at WinService_InstallUtil sample. You will need to schedule an elevated custom action and restart your service by either using .NET service API or with sc.exe. Wix#tasks already have sc.exe wrapped:
var project =
    new Project("My Product",
        new Dir(@"%ProgramFiles%\My Company\My Product",
            new File(@"..\SimpleService\MyApp.exe")),
        new ElevatedManagedAction(CustomActions.RestartService, Return.check,
...
[CustomAction]
public static ActionResult RestartService(Session session)
{
    return session.HandleErrors(() =>
    {
        Tasks.StopService("WixSharp.SimpleService", throwOnError:false);
        Tasks.StartService("WixSharp.SimpleService", throwOnError:false);        
    });
}
This can be even easier with Managed Setup:
project.AfterInstall += project_AfterInstall;
...
static void project_AfterInstall(SetupEventArgs e)
{
    Tasks.StopService("WixSharp.SimpleService", throwOnError:false);
    Tasks.StartService("WixSharp.SimpleService", throwOnError:false);
}
As for launch condition I advise you to have a look at Launch Condition sample. In your case that actual condition would be the presence of service exe, or another product being installed and product code being present in registry. I personally prefer to use C# to check the service presence from the event handler and abort the setup if needed.