Cannot start service from the command line or a debugger

146 Views Asked by At

I am developing a windows service project but I face different problems.

1.

Cannot start service from the command line or a debugger.

I already install the service by using installutil but after I install the service, it can start in mmc.

  1. I use this code before:
public void onDebug()
{
    OnStart(null);
}
#if DEBUG
            Service1 myService = new Service1();
            myService.onDebug();
            Thread.Sleep(System.Threading.Timeout.Infinite);
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Push_Message_Service()
            };
            ServiceBase.Run(ServicesToRun);
#endif

Because I want to debug in visual studio but I sometimes need to start the service in Service mmc. I don't want to build in Release folder when I need to start the service. So everyone please help me to fix the problems, thank you you guys!

1

There are 1 best solutions below

4
Dou Xu-MSFT On

I created a windows service project in visual studio 2022 and it popped the error message when i debugged the windows service.

enter image description here

It looks like you are not able to debug from code in visual studio without installing and starting your service. Were you encountering the same error message ? If so,please make sure you have installed your windows service by installutil and start your service. Please refer to How to start services

As you mentioned above, you have used #if DEBUG to debug windows service and it worked fine.

Or you can start the service through the Windows Service Control Manager and then attaching the debugger to the thread. For more information, please follow To debug a service

Here is a similar ticket:

Easier way to debug a Windows service

Hope it can help you.