Automatically start a Windows Service on install

前端 未结 13 1267
轻奢々
轻奢々 2020-11-28 19:13

I have a Windows Service which I install using the InstallUtil.exe. Even though I have set the Startup Method to Automatic, the service does not start when installed, I have

相关标签:
13条回答
  • 2020-11-28 19:37

    After refactoring a little bit, this is an example of a complete windows service installer with automatic start:

    using System.ComponentModel;
    using System.Configuration.Install;
    using System.ServiceProcess;
    
    namespace Example.of.name.space
    {
    [RunInstaller(true)]
    public partial class ServiceInstaller : Installer
    {
        private readonly ServiceProcessInstaller processInstaller;
        private readonly System.ServiceProcess.ServiceInstaller serviceInstaller;
    
        public ServiceInstaller()
        {
            InitializeComponent();
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new System.ServiceProcess.ServiceInstaller();
    
            // Service will run under system account
            processInstaller.Account = ServiceAccount.LocalSystem;
    
            // Service will have Automatic Start Type
            serviceInstaller.StartType = ServiceStartMode.Automatic;
    
            serviceInstaller.ServiceName = "Windows Automatic Start Service";
    
            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
            serviceInstaller.AfterInstall += ServiceInstaller_AfterInstall;            
        }
        private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            ServiceController sc = new ServiceController("Windows Automatic Start Service");
            sc.Start();
        }
    }
    }
    
    0 讨论(0)
  • 2020-11-28 19:37

    You corrupted your designer. ReAdd your Installer Component. It should have a serviceInstaller and a serviceProcessInstaller. The serviceInstaller with property Startup Method set to Automatic will startup when installed and after each reboot.

    0 讨论(0)
  • 2020-11-28 19:38

    Programmatic options for controlling services:

    • Native code can used, "Starting a Service". Maximum control with minimum dependencies but the most work.
    • WMI: Win32_Service has a StartService method. This is good for cases where you need to be able to perform other processing (e.g. to select which service).
    • PowerShell: execute Start-Service via RunspaceInvoke or by creating your own Runspace and using its CreatePipeline method to execute. This is good for cases where you need to be able to perform other processing (e.g. to select which service) with a much easier coding model than WMI, but depends on PSH being installed.
    • A .NET application can use ServiceController
    0 讨论(0)
  • 2020-11-28 19:44

    How about following commands?

    net start "<service name>"
    net stop "<service name>"
    
    0 讨论(0)
  • 2020-11-28 19:44

    You can use the following command line to start the service:

    net start *servicename*
    
    0 讨论(0)
  • 2020-11-28 19:48

    Here is a procedure and code using generated ProjectInstaller in Visual Studio:

    1. Create Windows Service project in Visual Studio
    2. Generate installers to the service
    3. Open ProjectInstaller in design editor (it should open automatically when installer is created) and set properties of generated serviceProcessInstaller1 (e.g. Account: LocalSystem) and serviceInstaller1 (e.g. StartType: Automatic)
    4. Open ProjectInstaller in code editor (press F7 in design editor) and add event handler to ServiceInstaller.AfterInstall - see the following code. It will start the service after its installation.

    ProjectInstaller class:

    using System.ServiceProcess;
    
    
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent(); //generated code including property settings from previous steps
            this.serviceInstaller1.AfterInstall += Autorun_AfterServiceInstall; //use your ServiceInstaller name if changed from serviceInstaller1
        }
    
        void Autorun_AfterServiceInstall(object sender, InstallEventArgs e)
        {
            ServiceInstaller serviceInstaller = (ServiceInstaller)sender;
            using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName))
            {
                sc.Start();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题