2

I have created a Service using Microsoft's walkthrough.

It works. But I don't understand how. The Main() method doesn't seem to call the ProjectInstaller constructor (which looks like what installs the service). In fact, ProjectInstaller isn't referenced anywhere in the solution (I checked using Shift+F12).

So how is it executed?

EDIT

I'm not using installutil. Program's Main() calls ManagedInstallerClass.InstallHelper(args);.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • I suspect `installutil.exe` looks for it –  Apr 11 '18 at 14:37
  • @MickyD I'm not using installutil. `Program`'s `Main()` calls `ManagedInstallerClass.InstallHelper(args);`. – ispiro Apr 11 '18 at 14:38
  • Well you should be using installutil. How do you expect to install the service otherwise? –  Apr 11 '18 at 14:43
  • @MickyD See https://stackoverflow.com/questions/1449994/inno-setup-for-windows-service/1450051#1450051 . I'm using it because it's way simpler. – ispiro Apr 11 '18 at 14:44
  • installutil.exe is shipped with .NET. C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe –  Apr 11 '18 at 14:51

1 Answers1

3

Program which you use to install that service (installutil.exe) inspects your assembly and runs installers. As stated in its documentation:

Installutil.exe uses reflection to inspect the specified assemblies and to find all Installer types that have the System.ComponentModel.RunInstallerAttribute attribute set to true.

The tool then executes either the Installer.Install or the Installer.Uninstall method on each instance of the Installer type. Installutil.exe performs installation in a transactional manner; that is, if one of the assemblies fails to install, it rolls back the installations of all other assemblies. Uninstall is not transactional.

Update: You said you do not use installutil and instead using ManagedInstallerClass.InstallHelper. Reading documentation of this class:

This API supports the product infrastructure and is not intended to be used directly from your code.

Handles the functionality of the Installutil.exe (Installer Tool).

So first you should not be using it, and second - it does the same as installutil (so related to this question - uses reflection to inspect your assembly and run installers). I suppose that installutil tool actually just calls this method. Note that you have to pass path to your assembly in args of ManagedInstallerClass.InstallHelper, that's how it knows which assembly to inspect with reflection and run installer for.

ispiro
  • 26,556
  • 38
  • 136
  • 291
Evk
  • 98,527
  • 8
  • 141
  • 191
  • I'm not using installutil. `Program`'s `Main()` calls `ServiceBase.Run(services);`. – ispiro Apr 11 '18 at 14:39
  • 2
    @ispiro and how do you install your service then? Tutorial you linked uses `installutil` for that. – Evk Apr 11 '18 at 14:41
  • Sorry, wrong method: `ManagedInstallerClass.InstallHelper(args);` is what I meant. – ispiro Apr 11 '18 at 14:42
  • So you're saying that when I call it, it finds `ProjectInstaller` by it being in the assembly, and then runs it? – ispiro Apr 11 '18 at 14:48
  • `Note that you have to pass path to your assembly` - correct. That's what I'm doing. If not it wouldn't work. – ispiro Apr 11 '18 at 14:49
  • Yes it finds `ProjectInstaller` in the assembly which path you pass to it. In the example you linked, they do: `ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });`. So it loads assembly you passed (your current assembly actually), finds installer, then runs it. – Evk Apr 11 '18 at 14:49
  • Perfect. Thanks! – ispiro Apr 11 '18 at 14:51