Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a Windows Service as a console app

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

like image 275
pinki Avatar asked Mar 01 '11 16:03

pinki


People also ask

How do I run a Windows Service in Visual Studio?

Start Visual Studio with administrative credentials so you can attach to system processes. (Optional) On the Visual Studio menu bar, choose Tools, Options. In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button.

What is the difference between Console Application and Windows Service?

The key difference between a process running as an app versus as a service is that the service can operate entirely outside the normal association with a user and session. Thus services can run such that they start before any user logs in and can continue running after users log off.


1 Answers

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.

like image 105
Chris Wenham Avatar answered Sep 25 '22 13:09

Chris Wenham