Inno Setup for Windows service?

C#Windows ServicesInno Setup

C# Problem Overview


I have a .Net Windows service. I want to create an installer to install that windows service.

Basically, it has to do the following:

  1. Pack installutil.exe (Is it required?)
  2. Run installutil.exe MyService.exe
  3. Start MyService

Also, I want to provide an uninstaller which runs the following command:

installutil.exe /u MyService.exe

How to do these using Inno Setup?

C# Solutions


Solution 1 - C#

You don't need installutil.exe and probably you don't even have rights to redistribute it.

Here is the way I'm doing it in my application:

using System;
using System.Collections.Generic;
using System.Configuration.Install; 
using System.IO;
using System.Linq;
using System.Reflection; 
using System.ServiceProcess;
using System.Text;

static void Main(string[] args)
{
    if (System.Environment.UserInteractive)
    {
        string parameter = string.Concat(args);
        switch (parameter)
        {
            case "--install":
                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                break;
            case "--uninstall":
                ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                break;
        }
    }
    else
    {
        ServiceBase.Run(new WindowsService());
    }
}

Basically you can have your service to install/uninstall on its own by using ManagedInstallerClass as shown in my example.

Then it's just matter of adding into your InnoSetup script something like this:

[Run]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--install"

[UninstallRun]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--uninstall"

Solution 2 - C#

Here's how i did it:

Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

Apparently, Inno setup has the following constants for referencing the .NET folder on your system:

  • {dotnet11}
  • {dotnet20}
  • {dotnet2032}
  • {dotnet2064}
  • {dotnet40}
  • {dotnet4032}
  • {dotnet4064}

More information available here.

Solution 3 - C#

You can use

Exec(
    ExpandConstant('{sys}\sc.exe'),
    ExpandConstant('create "MyService" binPath= {app}\MyService.exe start= auto DisplayName= "My Service" obj= LocalSystem'), 
    '', 
    SW_HIDE, 
    ewWaitUntilTerminated, 
    ResultCode
    )

to create a service. See "sc.exe" on how to start, stop, check service status, delete service, etc.

Solution 4 - C#

If you want to avoid reboots when the user upgrades then you need to stop the service before copying the exe and start again after.

There are some script functions to do this at Service - Functions to Start, Stop, Install, Remove a Service

Solution 5 - C#

have a look at topshelf http://topshelf-project.com/

  • it lets you develop your service as a console application

  • adds a start/stop service as an API to your service...

  • ... that you can call from InnoSetup

    [Run] Filename: "{app}\myservice.exe"; Parameters: "stop" ; Flags : waituntilterminated Filename: "{app}\myservice.exe"; Parameters: "uninstall" ; Flags : waituntilterminated Filename: "{app}\myservice.exe"; Parameters: "install -description ""myservice""" ; Flags : waituntilterminated

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsoftwarematterView Question on Stackoverflow
Solution 1 - C#lubos haskoView Answer on Stackoverflow
Solution 2 - C#breezView Answer on Stackoverflow
Solution 3 - C#StevenView Answer on Stackoverflow
Solution 4 - C#Tony EdgecombeView Answer on Stackoverflow
Solution 5 - C#f.capetView Answer on Stackoverflow