运行Windows服务作为一个控制台应用程序

问题描述:

我要调试Windows服务,但它会弹出一个错误信息说

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

无法从命令启动服务
  线或调试。 Windows服务
  必须使用安装
  installutil.exe然后开始
  服务器资源管理器,Windows服务
  管理工具或NET START
  命令。

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.....

Windows服务之前可以运行,它必须安装首先使用installutil。 EG:

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

然后就可以打开服务列表中启动它。 EG:

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


  1. 右键点击我的电脑

  2. 单击在管理

  3. 打开了服务和应用程序

  4. 单击在服务

  5. 找到您的服务列表中,并在其上​​单击鼠标右键

  6. 点击开始

一旦开始,你可以去到Visual Studio中,单击调试,然后单击附加到进程。

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

另一种方法是这一行添加到您的OnStart()方法,在服务:

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

System.Diagnostics.Debugger.Launch();

当你做到这一点,它会提示你选择Visual Studio中的一个实例来调试服务。

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