2

Which of the two of these are preferable (and why) from the service installer, I've seen both mentioned on different websites (and here on stackoverflow Automatically start a Windows Service on install and How to automatically start your service after install?).

// Auto Start the Service Once Installation is Finished.
this.AfterInstall += (s, e) => new ServiceController("service").Start();
this.Committed += (s, e) => new ServiceController("service").Start();
Community
  • 1
  • 1
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69

3 Answers3

3

I consider the latter a little more proper (although a quick check of my codebase and I coded essentially the former). The difference I see is the chance of a Rollback happening. In the commit phase you are past the risk of a rollback. But if you start your service in the AfterInstall (which is just part of the overall Install phase (the four phases being Install, Rollback, Commit, Uninstall)) you have the possibility of a Rollback being issued by a subsequent InstallerClass. You would then need to stop your service and uninstall it (which Microsoft's default service installer classes do for you so it's not much of an issue.

In summary, there isn't too much of a difference.

Russell McClure
  • 4,821
  • 1
  • 22
  • 22
  • A little bit wrong: _[[AfterInstall](http://msdn.microsoft.com/en-us/library/system.configuration.install.installer.afterinstall.aspx)] Occurs after the Install methods of all the installers in the Installers property have run._ The event isn't related to any single installation component. – Grant Thomas Nov 11 '11 at 18:13
  • Not wrong at all. I've got extensive experience with ICs. ICs can be grouped together inside a single EXE/DLL as trees. The Installers property only applies to a single tree (ie the children of that particular IC). It says nothing about other trees of IC within the same EXE/DLL. Please give an example of how my comment is wrong. – Russell McClure Nov 11 '11 at 18:23
  • In that case, then the `Committed` event would behave just the same (since the instances of containers can't call those of other containers) - unless I'm misunderstanding you? – Grant Thomas Nov 11 '11 at 18:26
  • As I asked before, please give a clear explanation of how I'm wrong. Then I can more easily point out how your argument is wrong. For arguments sake lets take an EXE with 4 IC trees (IC1-IC4) and IC1 and IC3 have 2 child ICs each (IC1Sub1, IC1Sub2 etc). Are you clear on the execution order of the 4 phases ( I recommend you go crack open Microsofts class in your favorite decompiler )? – Russell McClure Nov 11 '11 at 18:33
  • Thanks for the help. Since committed is run last, seems most appropriate to me but wanted other opinions. – Chuck Savage Nov 15 '11 at 03:01
1

In C# in your service project you will have an installer class called ProjectInstaller.cs, modify it to override the AfterInstall event handler to autostart the service like the code below

[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
    public ProjectInstaller()
    {
        InitializeComponent();
    }

    protected override void OnAfterInstall(IDictionary savedState)
    {
        base.OnAfterInstall(savedState);
        using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName))
        {
            serviceController.Start();
        }
    }
}

This will automatically start your windows Service after installation

indago
  • 2,041
  • 3
  • 29
  • 48
1

Considering Committed is raised post-installation (that is, only when Install() calls have completed, and hence related events raised (if successful)) then I would say doing it at this point is "safest". in fact, I'm sure it is the last installation related event raised, and by doing so finalises the complete installation.

The Commit method is called only if the Install method of each installer in this instance's InstallerCollection succeeds.

Since Commit gathers information required for uninstallation, and it is possible to break and therefore for Rollback to be called throughout installation - you could possibly find yourself in a bind if services are already ambitiously running before completed, successful committing.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129