Subject: How to Auto Start Windows Service
Posted By: virendar23 Post Date: 10/31/2006 7:23:43 AM
Hai all, I have written windows service application.In StartType i selected automatic.But After i install the service it was not starting automatically.i should start it manually.Please Let me know what other settings should i make.

Thanks In Advance

Reply By: ejan Reply Date: 11/28/2006 6:02:04 AM
The StartType property specifies the behavior of a service at system startup. It doesn't address the issue of starting a service after you install a Windows Service using the Setup Project.

There are two ways you can address this issue: either instruct your user to restart the computer after installation or write an installer class for starting your service after it gets installed.

You will need to create a library with a class that derives from the System.Configuration.Install.Installer class. Decorate your class with [RunInstaller(true)]. Now override the OnCommitted method and write the following code inside the method:

try
{
  string serviceName = "<your_service_name>";
  ServiceController serviceController =
    new ServiceController(serviceName);
  serviceController.Start();
}
catch
{
 //Handle exception.
}

Compile the project and add its output to the Commit custom action of your Setup project that installs your service. This will start your service as soon as it's installed. Setting the StartType property to Automatic will ensure your service starts automatically each time the computer starts.

Hopefully this helps.

Cheers.

ejan
Reply By: MunishBhatia Reply Date: 12/26/2006 8:42:08 AM
i was also looking for the same answer!


thanks......
Reply By: sathai Reply Date: 2/3/2007 1:07:40 AM
ejan, thank you for the great tip. but need a bit more help.

when I uninstall the program I get a popup asking me do I want to stop the service I uninstalling. I try many ways, example

I created the override OnBeforeUninstall() here my code

        protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);
            try
            {
                ServiceController service = new ServiceController("SathaiDynIP");
                service.Stop();

                service.WaitForStatus(ServiceControllerStatus.Stopped);
            }
            catch
            {
                //Handle exception.
            }
        }

But it doesn't stop the sevices before uninstalling. Can you help?
Thank in advance.

JamesNW
Reply By: zawmn83 Reply Date: 4/12/2008 8:05:23 AM
I tried as your code in vb.net but it is not start once when I finish install.  Please help.

Public Overrides Sub Commit(ByVal savedState As IDictionary)
            Dim serviceName As String = "MyService"
            Dim serviceController As New System.ServiceProcess.ServiceController(serviceName)
            serviceController.Start()
    End Sub



quote:
Originally posted by ejan

The StartType property specifies the behavior of a service at system startup. It doesn't address the issue of starting a service after you install a Windows Service using the Setup Project.

There are two ways you can address this issue: either instruct your user to restart the computer after installation or write an installer class for starting your service after it gets installed.

You will need to create a library with a class that derives from the System.Configuration.Install.Installer class. Decorate your class with [RunInstaller(true)]. Now override the OnCommitted method and write the following code inside the method:

try
{
  string serviceName = "<your_service_name>";
  ServiceController serviceController =
    new ServiceController(serviceName);
  serviceController.Start();
}
catch
{
 //Handle exception.
}

Compile the project and add its output to the Commit custom action of your Setup project that installs your service. This will start your service as soon as it's installed. Setting the StartType property to Automatic will ensure your service starts automatically each time the computer starts.

Hopefully this helps.

Cheers.

ejan



Reply By: ahinson Reply Date: 4/18/2008 11:43:42 AM
Here's what you should enter:

<Security.Permissions.SecurityPermission(Security.Permissions.SecurityAction.Demand)> _
Public Overrides Sub Commit(ByVal savedState As System.Collections.IDictionary)

        MyBase.Commit(savedState)

        'Put the code to start your service here.
        Dim serviceName As String = "MyService"
        Dim serviceController As New System.ServiceProcess.ServiceController(serviceName)
        serviceController.Start()

    End Sub


quote:
Originally posted by zawmn83

I tried as your code in vb.net but it is not start once when I finish install.  Please help.

Public Overrides Sub Commit(ByVal savedState As IDictionary)
            Dim serviceName As String = "MyService"
            Dim serviceController As New System.ServiceProcess.ServiceController(serviceName)
            serviceController.Start()
    End Sub



quote:
Originally posted by ejan

The StartType property specifies the behavior of a service at system startup. It doesn't address the issue of starting a service after you install a Windows Service using the Setup Project.

There are two ways you can address this issue: either instruct your user to restart the computer after installation or write an installer class for starting your service after it gets installed.

You will need to create a library with a class that derives from the System.Configuration.Install.Installer class. Decorate your class with [RunInstaller(true)]. Now override the OnCommitted method and write the following code inside the method:

try
{
  string serviceName = "<your_service_name>";
  ServiceController serviceController =
    new ServiceController(serviceName);
  serviceController.Start();
}
catch
{
 //Handle exception.
}

Compile the project and add its output to the Commit custom action of your Setup project that installs your service. This will start your service as soon as it's installed. Setting the StartType property to Automatic will ensure your service starts automatically each time the computer starts.

Hopefully this helps.

Cheers.

ejan








Go to topic 70713

Return to index page 1