如何在 Visual Studio 中调试 Windows 服务?

如何在 Visual Studio 中调试 Windows 服务?

问题描述:

是否可以在 Visual Studio 中调试 Windows 服务?

Is it possible to debug the Windows services in Visual Studio?

我使用了类似的代码

System.Diagnostics.Debugger.Break();

但它给出了一些代码错误,例如:

but it is giving some code error like:

我收到两个事件错误:eventID 4096VsJITDebugger 和服务做了不响应启动或控制及时提出要求."

I got two event error: eventID 4096 VsJITDebugger and "The service did not respond to the start or control request in a timely fashion."

你也可以试试这个.

  1. 创建您的 Windows 服务并安装并启动....也就是说,Windows 服务必须在您的系统中运行.
  2. 在您的服务运行时,转到调试菜单,点击附加进程(或旧 Visual Studio 中的进程)
  3. 找到您正在运行的服务,然后确保选择了显示所有用户的进程显示所有会话中的进程,如果没有,则选择它.
  1. Create your Windows service and install and start…. That is, Windows services must be running in your system.
  2. While your service is running, go to the Debug menu, click on Attach Process (or process in old Visual Studio)
  3. Find your running service, and then make sure the Show process from all users and Show processes in all sessions is selected, if not then select it.

  1. 点击附加按钮
  2. 点击确定
  3. 点击关闭
  4. 在您想要的位置设置一个断点并等待执行.只要您的代码达到该点,它就会自动调试.
  5. 记住,把你的断点放在可达的地方,如果是onStart(),则停止并重新启动服务
  1. Click the Attach button
  2. Click OK
  3. Click Close
  4. Set a break point to your desirable location and wait for execute. It will debug automatic whenever your code reaches to that point.
  5. Remember, put your breakpoint at reachable place, if it is onStart(), then stop and start the service again

(经过大量的谷歌搜索,我在如何在 Visual Studio 中调试 Windows 服务"中找到了这个.)

(After a lot of googling, I found this in "How to debug the Windows Services in Visual Studio".)