Thursday 18 May 2017

Cannot start service from the command line or a debugger

Cannot start service from the command line or a debugger.  A Windows Service must first be installed (using installutil.exe) and then started with the ServerExplorer, Windows Services Administrative tool or the NET START command

When you run windows service debug mode from VS, i got above error.

Solution:

OOTB code:

 static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
             ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Scheduler()
            };
            ServiceBase.Run(ServicesToRun);    

        }
    }

Change to:


 static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            //ServiceBase[] ServicesToRun;
            //ServicesToRun = new ServiceBase[]
            //{              
            //    new Scheduler()
            //};
            //ServiceBase.Run(ServicesToRun);


            var myService = new Scheduler();
            if (Environment.UserInteractive)
            {
                MtProject.Scheduler objScheduler = new Scheduler();
                objScheduler.GetData();
            }
            else
            {
                var servicesToRun = new ServiceBase[] { myService };
                ServiceBase.Run(servicesToRun);
            }

        }
    }