Header add

In the previous article we see How to Create Windows Service in .NET Core. But one question is that we need to start the windows service manually, here we will see how to start the windows service start automatically whether it is windows Service or .NET Core Service.

How to Start Windows Service automatically ?

To start windows service automatically after installation following below steps.

  • We add OnAfterInstaller in the ProjectInstaller.cs File.
  • AfterInstall event handler triggers immediately after Windows Service is installed
    protected override void OnAfterInstall(IDictionary savedState)
            {
                base.OnAfterInstall(savedState);
    
                //The following code starts the services after it is installed.
                using (System.ServiceProcess.ServiceController serviceController = 
                    new System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName))
                {
                    serviceController.Start();
                }
            }
Code Explanation >>
>> Override the after installer class and the service controller make service run 
automatically once the installation is complete. 



 Summary
   In this tutorial we discussed how to Start Windows Service automatically after installation. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post