2

Possible Duplicate:
How to automatically start your service after install?

I have a Visual Studio 2008 C# .NET 3.5 service installer project (MSI) running on Windows 7 x64.

I subscribe to the ServiceInstaller.OnAfterInstall notification to start my service when the installation finishes.

[RunInstaller(true)]
public partial class MyInstaller : Installer
{
    private System.ServiceProcess.ServiceInstaller my_installer_;

    private void InitializeComponent()
    {
        // ...
        this.my_installer_.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.OnAfterInstall);
        // ...
    }

    private void OnAfterInstall(object sender, InstallEventArgs e)
    {
        using (System.ServiceProcess.ServiceController svc =
            new System.ServiceProcess.ServiceController("MyService"))
        {
            svc.Start(); // completes successfully
        }
    }
}

Though the function succeeds without exception, my service is never running when the installer finishes.

The event log shows no failures related to service startup and if I go to the services manager, I can start the service manually (or restart the PC and it will start automatically).

What do I need to do to automatically start my service when the installer process finishes?

Community
  • 1
  • 1
PaulH
  • 7,759
  • 8
  • 66
  • 143
  • It looks like what you've done should work. Possible trick to debug this: Try inserting a `Thread.Sleep` with a value of 30 seconds or so. It might allow you to attach a debugger to the service process, and you could set a break point right after the `Thread.Sleep`. Once you're paused in the debugger, if you still get errors on resume, then it isn't a timing thing. At that point you can query your service state at your leisure and determine what about that step is causing the problem. If the `Thread.Sleep` causes it to start working, it of course is a timing thing... – Merlyn Morgan-Graham Oct 27 '11 at 19:54
  • You can also add "System.Diagnostics.Debugger.Launch();" at the beginning of your start method to force the debugger to be attached, so that you can check what's going wrong. It's possible that there's an exception somewhere and your service simply stops automatically. – user276648 Sep 13 '12 at 01:48

2 Answers2

1

Using AfterInstall event

Create AfterInstall event in your Service Installer class and start service using ServiceController.

public ServiceInstaller()
{
    InitializeComponent();
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
    sc.Start();
}

Using Committed event

public ServiceInstaller()
{
    InitializeComponent();
    this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}

void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
    sc.Start();
}

Or you can override OnCommitted event

    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        base.OnCommitted(savedState);
        new ServiceController(serviceInstaller1.ServiceName).Start();
    }

Other than above please check following

  • Installer Start type : Automatic
  • Account :Local System

Other than the service installer you need to have setup project which created by giving primary output of above service installer.

enter image description here

in the setup create Custom action at least on install by giving service installer project output.

enter image description here

More information from here.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Damith
  • 62,401
  • 13
  • 102
  • 153
0

I assume that Start returns immediatly, and Starts the Service in the background. Check the Docs: http://msdn.microsoft.com/en-us/library/yb9w7ytd.aspx

Andreas
  • 6,447
  • 2
  • 34
  • 46
  • Yes, I'm sure it does. That doesn't explain why the service isn't started or how to start it correctly. – PaulH Oct 27 '11 at 19:01
  • Have you tried to wait until it is started? There's an example in the link that I've posted – Andreas Oct 27 '11 at 19:16
  • Waiting did not fix the issue - the installer hung on that wait. The service would attempt to start and fail. I had `base.Install(savedState);` at the top of my `Installer.Install` override. So, the installer was attempting to start the service before it had finished installing all dependencies. By moving the base call to the bottom of my override, I fixed the issue. Not something anybody could have seen from what I posted, so you get the points because you lead me to the solution. – PaulH Oct 27 '11 at 20:55