Getting Your Installer to Register a Service with Wix#

We use the amazing TopShelf to setup and configure our windows services. It makes it a snap to install your windows service into the service control manager. Simply issue the following commands against your exe and it is installed or uninstalled:

MyAwesomeService.exe /install
MyAwesomeService.exe /uninstall
This makes deployment pretty brain dead, but we want to make the process even easier by including a deployment installer. As mentioned previously, creating a installer with Wix# is a snap. And performing actions during the install couldn’t be simpler.

Here is the simple build definition for my current project:

public BuildResult Build()
{
    WixObject[] installables = GetInstallables();
    var project = new Project(string.Format(ProductName, buildEnvironment.Version), installables)
                      {
                          GUID = ProductId,
                          MSIFileName = string.Format(ProductName, buildEnvironment.Version),
                          UI = WUI.WixUI_ProgressOnly,
                          Manufacturer = Manufacturer,
                          SourceBaseDir = buildEnvironment.OutPutDirectory,
                          AutoAssignedInstallDirPath = InstallPath,
                          Properties = GetProperties(),
                          Actions = GetActions()
                      };

    string message = Compiler.BuildMsi(project);

    return new BuildResult { WasSuccessful = true, Message = message };
}

Notice, I set the project’s Actions property to the value returned by the method GetActions(). This is how I execute my install and uninstall commands during the installation process. The implementation of the method looks like this:

private Action[] GetActions()
{
    return new[]
               {
                   new InstalledFileAction("SomeCompany.SomeProduct.Service.exe", "/install", Return.check,
                                           When.After, Step.InstallFinalize, Condition.NOT_Installed),
                   new InstalledFileAction("SomeCompany.SomeProduct.Service.exe", "/uninstall", Return.check,
                                           When.Before, Step.InstallFinalize, Condition.Installed)
               };
}

And in the words of my good friend and coworker Eric Ridgeway, “That’s hot.”

Follow me on Mastodon!