Cannot start service from the command line or debugger Cannot start service from the command line or debugger windows windows

Cannot start service from the command line or debugger


Change the Main method in Program class as follows:

    /// <summary>    ///   The main entry point for the application.    /// </summary>    private static void Main()    {        var myService = new MyService();        if (Environment.UserInteractive)        {            Console.WriteLine("Starting service...");            myService.Start();            Console.WriteLine("Service is running.");            Console.WriteLine("Press any key to stop...");            Console.ReadKey(true);            Console.WriteLine("Stopping service...");            myService.Stop();            Console.WriteLine("Service stopped.");        }        else        {            var servicesToRun = new ServiceBase[] { myService };            ServiceBase.Run(servicesToRun);        }    }

You have to add a Start method to your service class:

    public void Start()    {        OnStart(new string[0]);    }

Change the output type of the project to 'Console Application' instead of 'Windows Application' in the 'Application' tab of the project properties. Now you can just press F5 to start debugging but you can still run the executable as a Windows Service.


Goto App.config

Find

<setting name="RunAsWindowsService" serializeAs="String"><value>True</value>

Set to False


Press CTRL-ALT-CANC (*), and go to Services tab. There is a list of services, search the one you need to start, select it and click "start".If it's not there, maybe it was uninstalled, not (correctly?) installed, or for some other reason your service is not known by Windows.

(*) or CTRL-ALT-DEL(ete) or others, depending by the keyboard language.