VB.NET - start window service automatically not working

Asked By kiruba .e on 20-Jun-12 09:31 AM
Earn up to 50 extra points for answering this tough question.
hi,

i wrote this code in my windowservice.


Its not working. what s my ques is, how to write code to start window service after my setup file installation. 

I dont want to use installuitl or anything. just i want to install the service only.  After installation it should start automatically.

 Private Sub ServiceInstaller1_Committed(ByVal sender As Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles ServiceInstaller1.Committed
      Dim controller = New ServiceController(Me.ServiceInstaller1.ServiceName)
      controller.Start()
      controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running)
    End Sub


thanks regards
kiruba.e
[)ia6l0 iii replied to kiruba .e on 20-Jun-12 12:16 PM
Please use the AfterInstall method, this works for me.  For your code sample, it will be as follows:

Private Sub ServiceInstaller1_AfterInstall(sender As Object, e As InstallEventArgs)
Dim sctlr As New ServiceController(Me.ServiceInstaller1.ServiceName)
sctlr.Start()
End Sub


If you still see it not started, look at the Event Viewer - there would be an error logged on why the service failed to initialize.  (Please type "eventvwr" in your run menu without quotes, to launch the event viewer. Please look below for a screen shot)



Hope this helps.
Jitendra Faye replied to kiruba .e on 21-Jun-12 12:41 AM
For this you need to add installer class and in that class you have to define some functionality like this-

Public ServiceInstaller()
{
    //... Installer code here
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
   //Code to start automatically 
    ServiceController sc = new ServiceController("ServiceName");
    sc.Start();
}

Try this and let me know;

TSN ... replied to kiruba .e on 21-Jun-12 12:44 AM

In your installer class just add the event handler for the afterinstall ,

public ServiceInstaller()

{

    //... Installer code here

    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);

}

 

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)

{

    ServiceController sc = new ServiceController("ServiceName");

    sc.Start();

}

kiruba .e replied to TSN ... on 21-Jun-12 03:25 AM
as im using vb.net it shows error. can u give that in vb.net pls.
TSN ... replied to kiruba .e on 21-Jun-12 05:19 AM

hi…

here is the code in VB.Net


Public Sub New()

        '... Installer code here

        Me.AfterInstall += New InstallEventHandler(AddressOf ServiceInstaller_AfterInstall)

End Sub

Private Sub ServiceInstaller_AfterInstall(sender As Object, e As InstallEventArgs)

        Dim sc As New ServiceController("ServiceName")

        sc.Start()

End Sub

Vikram Singh Saini replied to kiruba .e on 21-Jun-12 11:42 PM
Along with other tech guys suggestions, I would recommend you to follow steps:

1. Open service class design window and add installer for service.
2. Right click serviceInstaller's instance serviceInstaller1 (by default) --> Properties
3. Set StartType (Indicates how and when this service is started) to Automatic.
4. Build and install the service. Now onwards the service would start automatically after installation.