如何为Windows服务设置调试环境 [英] How to set up debug environment for windows service

查看:117
本文介绍了如何为Windows服务设置调试环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我必须为调用Windows服务的Windows应用程序设置调试环境。我无法调试Windows服务。实际上我的服务文件夹(Services.msc)中没有服务。如何手动创建?

有谁能帮我解决这个问题?





谢谢

Antoni

Hi All,
I have to set up an debugging environment for an Windows application which calls an Windows service. I am not able to debug windows service. Actually I don't have service in my Services folder (Services.msc). How to create manually?
Can anyone help me to solve this?


Thanks
Antoni

推荐答案

您不需要或可以拥有任何特殊环境,但调试Windows服务并不容易。



有一件事你可以做到只是在Visual Studio下编译它代替部署,使用服务控制器以常规方式启动它,然后在Visual Studio中使用Debug - >附加过程。执行将在您可以提前设置的某些断点处停止,然后您可以继续步骤调试。这在这里描述:

http:/ /msdn.microsoft.com/en-us/library/7a50syb3%28v=vs.110%29.aspx [ ^ ]。



此方法是很难调试执行Windows服务进程的早期步骤。为了涵盖这些步骤,您可以创建一个人工延迟。使用日志记录可以解决许多问题。请参阅: http://msdn.microsoft.com/en-us/library/vstudio /system.diagnostics.eventlog [ ^ ]。



我使用了一种不太常见且专注的调试方法。我开发了一种特殊类型的应用程序,它既可以作为交互式应用程序,也可以作为服务,具体取决于它的托管方式。如果从命令行管理程序启动应用程序,它将以交互方式执行,如果从服务小程序(由服务控制器)启动,则将其作为服务执行。对于调试功能而言重要的是:两种模式都使用相同的方法,这些方法设计用于两种环境。此外,对于交互模式,我创建了一个特殊的类似控制台的窗口(不是标准控制台,只是基于一些列表框)来显示日志记录信息,通过实现双重日志记录机制(在服务模式下,它使用 EventLog ,见上文。



对于这种技术,关键是通过这种双模式应用程序检测执行模式。这是通过使用静态属性 System.Environment.UserInteractive 来完成的:

http://msdn.microsoft.com/en-us/library/system.environment.userinteractive%28v=vs.110 %29.aspx [ ^ ]。



这种方法帮助我仅使用交互模式调试几乎所有问题。仅存在于服务模式中的问题也存在;所以我只使用日志来修复它们。值得注意的是,我使用这种技术开发的应用程序非常复杂。如果服务不是非常复杂,你可以在交互模式下走出去做几乎所有调试。







另请参阅我的双模服务应用程序上的过去答案:使用UI配置Windows服务 [ ^ ]。



关于安装问题,另请参阅:如何以编程方式安装Windows服务 [ ^ ]。



请参阅我在Windows服务创建和安装上的其他过去答案:

使用UI配置Windows服务 [ ^ ],

Windows服务错误 [ ^ ],

http://msdn.microsoft.com/en-us/library/zt39148a.aspx [ ^ ]。



-SA
You don't need or can have any special environment, but debugging of a Windows Service is not so easy.

One thing you can do it just compiling it under Visual Studio in place of deployment, start it in a regular way using the Service Controller, and then, in Visual Studio, use "Debug -> Attach Process". The execution will stop at some breakpoints you could set in advance, and then you can continue step be step debugging. This is described here:
http://msdn.microsoft.com/en-us/library/7a50syb3%28v=vs.110%29.aspx[^].

This method is hard to debug the earlier steps of execution of a Windows Service process. To cover those steps, you can, say, create an artificial delays. Many things can be resolved by using logging. Please see: http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.eventlog[^].

I've used one much less usual and dedicated approach to debugging. I developed a special type of application which can work as both an interactive application and as the service, depending how it is hosted. If you start the application from the Shell, it is executed in an interactive mode, and if you start it from "Services" applet (by a Service Controller), it is executed as service. Important for debugging feature is: both modes use the same methods, which are designed to work in both environments. Moreover, for an interactive mode, I created a special console-like window (not a standard console, just based on some list box) to show logging information, by implementing dual logging mechanism (in service mode, it uses EventLog, see above).

For this technique, the key is the detection of the execution mode by such dual-mode application. This is done by using the static property System.Environment.UserInteractive:
http://msdn.microsoft.com/en-us/library/system.environment.userinteractive%28v=vs.110%29.aspx[^].

This approach helped me to debug nearly all of the problems using just the interactive mode. The problems which were manifested only in the service mode also existed; so I fixed them using just the logging. It's important to note that the applications I developed using this techniques were extremely complicated. If the Service is not extremely complex, you could walk out doing nearly all debugging in the interactive mode.



See also my past answer on my dual-mode Service application: Configure Windows Service with UI[^].

On the problem of the installation, see also: How to install windows service programmatically[^].

Please see also my other past answers on Windows Service creation and installation:
Configure Windows Service with UI[^],
Windows services Error[^],
http://msdn.microsoft.com/en-us/library/zt39148a.aspx[^].

—SA


您的第一步是创建和安装服务 - http ://support.microsoft.com/kb/251192 [ ^ ]。
The first step for you would be to create and install a service - http://support.microsoft.com/kb/251192[^].


这篇关于如何为Windows服务设置调试环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆