2

I have created a windows service in Visual studio 2012 (c#) that needs to be started after install. I have read so many articles and StackOverflow questions but none got it working. In the main function, I have:

static void Main(string []args)
{
     ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
} 

I have registered AfterInstall event of the service.

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

I'm logged in as an administrator. When I run the .exe file (as administrator), it tries to installs the service (keeps it in starting status for 2 minutes) but fails to start it. When I run in debug mode, I get an exception on sc.Start(). The log file says:

 System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of System.ServiceProcess.ServiceInstaller.
 The inner exception System.InvalidOperationException was thrown with the following error message: Cannot start service Database Convertor on computer '.'.
The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: The service did not respond to the start or control request in a timely fashion.

I tried to change LocalService account to LocalSystem with no success. Then I tried changing the main function too

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
        { 
             new Service1() 
        };
ServiceBase.Run(ServicesToRun);

and when I installutil convertor.exe, it successfully installs and starts the service (but I need to start it through the program).

Why it starts the service when installing through installutil and why it throws an exception when I manually call installhelper?

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Ali Baig
  • 3,819
  • 4
  • 34
  • 47

1 Answers1

1

Try removing everything in the OnStart function in the service and see if it starts successfully, if it starts then debug the onStart function (You can user Debugger.Attach())

Best of luck

Ahmad Al Sayyed
  • 596
  • 5
  • 13
  • Not worked. Same issue:( I removed everything from onstart and onstop methods still.. – Ali Baig Sep 15 '14 at 10:15
  • Try to start your service OnCommitted not onAfterInstall – Ahmad Al Sayyed Sep 15 '14 at 11:27
  • I tried it that way too. Plus i tried overriding the install function of installer.cs but both didn't worked out too. – Ali Baig Sep 15 '14 at 13:53
  • If you remove service start (temporary) will the service be installed successfully? and after installing it can it be started successfully? – Ahmad Al Sayyed Sep 15 '14 at 13:59
  • Yes. When it installs it successfully when i comment out servicecontroller.start() but when i try to start it from services.msc it fails and says: Service didn't respond in timely fashion. However when i install the service through installutil it starts automatically after the installation. – Ali Baig Sep 15 '14 at 14:14
  • Check the user account used by the service after installation (without starting) and compare it with the user account when installing through installutil – Ahmad Al Sayyed Sep 15 '14 at 14:16
  • How can i do that when installing through installutil?:( – Ali Baig Sep 15 '14 at 15:25
  • You can check the user account from the services.msc, double click on the service and go to the "Log on" tab – Ahmad Al Sayyed Sep 15 '14 at 15:28
  • Ok. In both the cases, its "Local Service". Same code, when installed though installutil works, but don't when installed through ManagedInstallerClass.InstallHelper(new string[] { ExePath });. There might be any problems with some sort of permissions? – Ali Baig Sep 15 '14 at 15:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61294/discussion-between-ahmad-and-ali-baig). – Ahmad Al Sayyed Sep 15 '14 at 18:23