.net 无法调试windows服务

.net 无法调试windows服务,.net,windows-services,.net,Windows Services,我有一个windows服务,我使用 installutil windowsservice1.exe 此服务显示在服务面板中,我可以启动/停止它 但是,当我按F5调试它时,会出现以下错误 Cannot Start windows Service from the command line or debugger. A Windows Service must be installed...... 但是,它已经安装。那么为什么会出现此错误?您需要将Visual Studio调试器附加到它,如本文

我有一个windows服务,我使用

installutil windowsservice1.exe
此服务显示在
服务
面板中,我可以
启动/停止它

但是,当我按F5调试它时,会出现以下错误

Cannot Start windows Service from the command line or debugger. A Windows Service must be installed......

但是,它已经安装。那么为什么会出现此错误?

您需要将Visual Studio调试器附加到它,如本文所示

从以上链接:

由于服务必须在服务控制管理器的上下文中运行,而不是在Visual Studio中运行,因此调试服务不像调试其他Visual Studio应用程序类型那样简单。若要调试服务,必须启动该服务,然后将调试器附加到运行该服务的进程。然后,可以使用VisualStudio的所有标准调试功能调试应用程序


如果您对系统拥有正确的权限,请在
Main()
OnStart()
中调用
system.Diagnostics.Debugger.Launch()
。从SCM启动服务时,将显示一个对话框,允许您选择使用哪个Visual Studio实例调试服务。您的服务将在
Launch()
调用时暂停,就像遇到断点一样。您可以从那里进行正常调试。