How to start the .NET Windows service immediately after installation?

Besides the service .StartType = ServiceStartMode.Automatic my service does not start after installation

Decision

Paste this code into your ProjectInstaller

protected override void OnAfterInstall(System.Collections.IDictionary savedState) { base.OnAfterInstall(savedState); using (var serviceController = new ServiceController(this.serviceInstaller1.ServiceName, Environment.MachineName)) serviceController.Start(); } 

Thanks ScottTx and Francis B.

+81
installer windows-services
Jul 28 '09 at 17:13
source share
8 answers

You can do all this from the service executable in response to events triggered from the InstallUtil process. Override the OnAfterInstall event to use the ServiceController class to start the service.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx

+22
Jul 28 '09 at 17:42
source share

I posted a step-by-step procedure for creating a Windows service in C # here . It looks like you are at least up to this point, and now you are wondering how to start the service after installing it. Setting the StartType property to "Automatic" will cause the service to start automatically after rebooting your system, but it will not (as you discovered) automatically start your service after installation.

I don’t remember where I found it from (maybe Mark Gravell?), But I found a solution on the Internet that allows you to install and run your service, actually starting your service. Here step by step:

  • Structure the Main() function of your service as follows:

     static void Main(string[] args) { if (args.Length == 0) { // Run your service normally. ServiceBase[] ServicesToRun = new ServiceBase[] {new YourService()}; ServiceBase.Run(ServicesToRun); } else if (args.Length == 1) { switch (args[0]) { case "-install": InstallService(); StartService(); break; case "-uninstall": StopService(); UninstallService(); break; default: throw new NotImplementedException(); } } } 
  • Here is the support code:

     using System.Collections; using System.Configuration.Install; using System.ServiceProcess; private static bool IsInstalled() { using (ServiceController controller = new ServiceController("YourServiceName")) { try { ServiceControllerStatus status = controller.Status; } catch { return false; } return true; } } private static bool IsRunning() { using (ServiceController controller = new ServiceController("YourServiceName")) { if (!IsInstalled()) return false; return (controller.Status == ServiceControllerStatus.Running); } } private static AssemblyInstaller GetInstaller() { AssemblyInstaller installer = new AssemblyInstaller( typeof(YourServiceType).Assembly, null); installer.UseNewContext = true; return installer; } 
  • Continued with supporting code ...

     private static void InstallService() { if (IsInstalled()) return; try { using (AssemblyInstaller installer = GetInstaller()) { IDictionary state = new Hashtable(); try { installer.Install(state); installer.Commit(state); } catch { try { installer.Rollback(state); } catch { } throw; } } } catch { throw; } } private static void UninstallService() { if ( !IsInstalled() ) return; try { using ( AssemblyInstaller installer = GetInstaller() ) { IDictionary state = new Hashtable(); try { installer.Uninstall( state ); } catch { throw; } } } catch { throw; } } private static void StartService() { if ( !IsInstalled() ) return; using (ServiceController controller = new ServiceController("YourServiceName")) { try { if ( controller.Status != ServiceControllerStatus.Running ) { controller.Start(); controller.WaitForStatus( ServiceControllerStatus.Running, TimeSpan.FromSeconds( 10 ) ); } } catch { throw; } } } private static void StopService() { if ( !IsInstalled() ) return; using ( ServiceController controller = new ServiceController("YourServiceName")) { try { if ( controller.Status != ServiceControllerStatus.Stopped ) { controller.Stop(); controller.WaitForStatus( ServiceControllerStatus.Stopped, TimeSpan.FromSeconds( 10 ) ); } } catch { throw; } } } 
  • At this point, after installing the service on the target machine, simply start your service from the command line (like any regular application) with the -install command line argument to install and start your service.

I think I reviewed everything, but if you find that this does not work, let me know so that I can update the answer.

+150
Jul 28 '09 at 17:45
source share

Visual studio

If you create an installation project using VS, you can create a custom action that called the .NET method to start the service. But in fact, it is not recommended to use managed user actions in MSI. See page .

 ServiceController controller = new ServiceController(); controller.MachineName = "";//The machine where the service is installed; controller.ServiceName = "";//The name of your service installed in Windows Services; controller.Start(); 

InstallShield or Wise

If you use InstallShield or Wise, these applications provide the ability to run the service. For example, with Wise, you need to add a service control action. In this step, you indicate whether you want to start or stop the service.

Wix

Using Wix, you need to add the following xml code under your service component. For more information about this, you can check this page .

 <ServiceInstall Id="ServiceInstaller" Type="ownProcess" Vital="yes" Name="" DisplayName="" Description="" Start="auto" Account="LocalSystem" ErrorControl="ignore" Interactive="no"> <ServiceDependency Id="????"/> ///Add any dependancy to your service </ServiceInstall> 
+7
Jul 28 '09 at 17:38
source share

You need to add a custom action at the end of the "ExecuteImmediate" sequence in MSI, using the name of the EXE component or package (sc start) as the source. I do not think that this can be done using Visual Studio, you may have to use a real tool for creating MSI for this.

+6
Jul 28 '09 at 17:20
source share

To start it right after installation, I create a batch file with installutil followed by sc start

It is not perfect, but it works ....

+5
Jul 28 '09 at 17:20
source share

Use the .NET ServiceController class to start it, or issue a command line command to start it - "net start servicename". In any case, it works.

+5
Jul 28 '09 at 17:21
source share

To add ScottTx to the answer, here is the actual code to start the service if you do this the Microsoft path (i.e. using Setup project, etc.)

(sorry VB.net code, but this is what I'm stuck with)

 Private Sub ServiceInstaller1_AfterInstall(ByVal sender As System.Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles ServiceInstaller1.AfterInstall Dim sc As New ServiceController() sc.ServiceName = ServiceInstaller1.ServiceName If sc.Status = ServiceControllerStatus.Stopped Then Try ' Start the service, and wait until its status is "Running". sc.Start() sc.WaitForStatus(ServiceControllerStatus.Running) ' TODO: log status of service here: sc.Status Catch ex As Exception ' TODO: log an error here: "Could not start service: ex.Message" Throw End Try End If End Sub 

To create the above event handler, go to the ProjectInstaller constructor, where there are 2 controls. Click on the element ServiceInstaller1. Go to the event properties window and there you will see the AfterInstall event.

Note. Do not put the above code in the AfterInstall event for ServiceProcessInstaller1. This will not work based on experience. :)

+4
Oct 27 '11 at 17:13
source share

The easiest solution is found here install-windows-service-without-installutil-exe by @ Hoàng Long

 @echo OFF echo Stopping old service version... net stop "[YOUR SERVICE NAME]" echo Uninstalling old service version... sc delete "[YOUR SERVICE NAME]" echo Installing service... rem DO NOT remove the space after "binpath="! sc create "[YOUR SERVICE NAME]" binpath= "[PATH_TO_YOUR_SERVICE_EXE]" start= auto echo Starting server complete pause 
-one
Apr 03 '17 at 15:31 on
source share



All Articles