如何调试.NET Windows服务的OnStart方法? [英] How to debug the .NET Windows Service OnStart method?

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

问题描述

我有code .NET编写时,作为Windows服务安装,只有失败。该故障不会使服务甚至开始。我想不通我怎么可以进入OnStart方法。

I have code written in .NET that only fails when installed as a Windows service. The failure doesn't allow the service to even start. I can't figure out how I can step into the OnStart method.

如何:调试Windows服务应用程序 的给出了一个诱人的线索:

How to: Debug Windows Service Applications gives a tantalizing clue:

附加到该服务的过程中,您可以调试大多数但不是所有的服务的code;例如,因为服务已经启动,则无法调试code在该服务的OnStart方法这种方式,或code在用于加载该服务的主要方法。 以解决此的一种方式是只存在于调试,以帮助您的服务应用程序创建一个临时的第二个服务。您可以同时安装服务,然后启动这个虚拟服务加载服务进程。一旦临时服务已经启动的过程中,你就可以使用Debug菜单在Visual Studio附加到服务进程。

Attaching to the service's process allows you to debug most but not all of the service's code; for example, because the service has already been started, you cannot debug the code in the service's OnStart method this way, or the code in the Main method that is used to load the service. One way to work around this is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this "dummy" service to load the service process. Once the temporary service has started the process, you can then use the Debug menu in Visual Studio to attach to the service process.

不过,我并不清楚是怎么回事正是你应该创建虚拟服务加载服务进程。

However, I'm not clear how it is exactly that you are supposed to create the dummy service to load the service process.

推荐答案

有一件事情你可以做一个临时的解决方法是启动调试器为code。在OnStart中的第一行

One thing you could do as a temporary workaround is to launch the debugger as the first line of code in the OnStart

System.Diagnostics.Debugger.Launch()

这会提示您输入您要使用调试器。只要有解决方案在Visual Studio已经打开,从列表中选择该实例。

This will prompt you for the debugger you'd like to use. Simply have the solution already open in Visual Studio and choose that instance from the list.

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

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