C# – Run a Windows Service as a console app

cwindows-services

I want to debug a Windows service but it pops an error message saying

Cannot start service from the command
line or a debugger. A windows service
must be installed using
installutil.exe and then started with
the Server explorer, windows services
Administrative tools or the NET start
command.

I don't really have any idea about this error…..

enter image description here

Best Answer

Before a Windows Service can run, it has to be "installed" first using installutil. EG:

C:\installutil -i c:\path\to\project\debug\service.exe

Then you can open up the list of Services to start it. EG:

  1. Right click 'My Computer'
  2. Click on 'Manage'
  3. Open up 'Services and Applications'
  4. Click on 'Services'
  5. Find your service in the list and right-click on it
  6. Click on 'Start'

Once it has started, you can go into Visual Studio, click on 'Debug', then click on 'Attach to Process'.

Another technique is to add this line to your OnStart() method in the service:

System.Diagnostics.Debugger.Launch();

When you do that, it'll prompt you to pick an instance of Visual Studio to debug the service in.

Related Topic