我如何调试Windows服务 [英] how do i debug the Windows Service

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

问题描述

大家好,我已经使用c#,vs 2008创建了一个Windows服务.它已成功构建并部署.我正在尝试调试该服务.方法是按debug-> attach process->并附加该服务所有的断点都被禁用.我正在以管理员模式运行vs2008.如何调试该服务?

Hi All,I have created a windows service using c#,vs 2008.it is build and deployed successfully.i am trying to debug the service.by pressing debug-->attach process-->and attaching the service all the breakpoints disabled.i am running vs2008 in admin mode.how do i debug the service?

推荐答案

确实很简单地使包含该服务的可执行文件具有双重功能:作为服务运行,以及作为控制台应用程序运行.在此后一种情况下,它与任何其他控制台应用程序一样容易调试.顺便说一句,它会自动安装.
我最初在这里找到它: https://groups.google.com/forum/?hl=zh-CN&fromgroups#!topic/microsoft.public.dotnet.languages.csharp/TUXp6lRxy6Q [我的文章中阅读起来要容易一些. [ ^ ] i>服务部分.
It is really simple do make the executable assambly containing the service to have dual functionality: to run as service, and to run as console application. In this later case it is as easy to debug as any other console application. And by the way the assambly will be self-installing.
I found it originally here: https://groups.google.com/forum/?hl=en&fromgroups#!topic/microsoft.public.dotnet.languages.csharp/TUXp6lRxy6Q[^], see Marc Gravell''s post. I have made my adaptation of it, a little easyer to read in my article[^] under The service section.


您需要在Windows服务启动中添加延迟,以便有足够的时间附加到该进程.

You need to add a delay in the windows service start-up to give you enough time to attach to the process.

protected override void OnStart(string[] args)
{
        #if DEBUG
	// When debugging a service, you have to attach to a running process. This gives me 
	// time to attach for debugging to slowdown startup so I can set a breakpoint. 
	System.Threading.Thread.Sleep(15000);
	#endif
}


CodeProject文章中介绍了一种专业的解决方案:
A professional solution is described in a CodeProject article: Debugging Windows Services under Visual Studio .NET[^]


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

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