Run a Windows Service as a console app

前端 未结 6 869
别跟我提以往
别跟我提以往 2020-12-02 12:30

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

6条回答
  •  盖世英雄少女心
    2020-12-02 12:54

    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.

提交回复
热议问题