Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

C:installutil -i c:pathoprojectdebugservice.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...