Search code examples
c#windows-serviceswindows-installerinstallutil

An exception occurred in the OnAfterInstall event handler of the Installer


I have created a windows service, that I am trying to deploy on the server.

And trying to install it using Command Prompt, with Administrator role.

Installer:

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

    protected override void OnAfterInstall(IDictionary savedState)
    {
        base.OnAfterInstall(savedState);

        //The following code starts the services after it is installed.
        using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName))
        {
            serviceController.Start();
        }
    }

    private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {
        //this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
    }

    private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {

    }
}

And it throws error as,

error in CMD

an exception occurred in the onafterinstall event handler

and also,

System.InvaldiOperationException: Cannot start service on Computer. etc.

Any prompt solutions?
Similar posts: Post-1, Post-2


Solution

  • That error message about the service "not responding in a timely manner" suggests that your service is broken in some way. The Start mechanism is not a fire-and-forget design, it's more like a call into your service startup code. The service is expected to exit its start code (in a timely manner) to indicate that it's running. It's likely that your start code is doing too much work inline instead of just doing basic initialization and then instantiating a thread to do the main work of the service OR it's hanging up somewhere.