如何在不安装的情况下调试Windows服务 [英] How to debug windows service without installing

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

问题描述

我删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题.i删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题。我删除了我的问题。

i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.i deleted my question.

推荐答案

你大部分都在那里,你只需要作弊。



记住范围,你有几个选择如何评价这个。因为我使用测试夹具,所以我通常使用并行执行过程,因此我不会从调试函数调用OnStart。在你的情况下,它看起来像这样:



You're most of the way there, you just need to cheat a little.

Keeping scope in mind, you have a couple of options on how to appraoch this. Because I use test fixtures, I usually use a parallel execution process so that I'm not calling OnStart from my debug function. In your case, it would look like this:

public void test()
{
   System.Diagnostics.EventLog.WriteEntry("ActiveDirectoryService","Active Directory Service Started");

   TrialChecker trial = new TrialChecker();
           
   if (!trial.TrialCheck()) 
   {
      Manager.Intialize(args, typeof(ADService));
   }
   
   while(true){} // Keep going until debug process is stopped.
}





另外,从你的Main()中取出Thread.Sleep。



Also, take the Thread.Sleep out of your Main().


我不知道为什么你之前决定代码 if(#DEBUG)可以被视为等同于之后的代码。如果您尝试以交互方式运行它,如果在 else 之后设置断点,您的调试器将会停止,但是如果您将其作为服务控制器运行,则不会得到在那里,由于以下原因:您必须首先启动服务,然后使用Visual Studio的 D ebug / Attach to P rocess。在调试运行的过程之前,您无法执行此操作,但是当您执行此操作时,该过程很可能会通过第二行并进入无限等待状态。将附加调试器,但进程将保持等待状态。它完全没有意义。



基本上,您有两种调试方法:将调试器附加到进程或交互式。要附加调试器,您可以在要调试的线程中等待足够的时间,而不是无限期,然后,在启动进程时,在睡眠调用之后设置断点。这很无聊,但有时它会对你有所帮助。



您还可以调试交互式启动过程中的大多数问题。为此,您可以使用以下事实:根据流程的启动方式,您可以编写略有不同的行为。如果(DEBUG),我不会触及。相反,您可以使用相同的流程代码,而不依赖于任何预编译器设置。要从服务中告知交互模式,可以使用静态变量 System.Environment.UserInteractive 。我基于这个标志开发了一种完整的技术;它允许我以交互方式调试99%的问题,您在问题中询问的方式。当然,应该使用无线程睡眠



如需进一步的想法,请参阅我过去的回答:使用UI配置Windows服务 [ ^ ]。



-SA
I have no idea why did you decide that the code before "if(#DEBUG)" can be considered equivalent to the code after. If you tried to run it interactively, your debugger would really stop if you set a breakpoint after "else", but it you run it as a service controller, you would not get there, by the following reason: you would have to start the service first and then use Visual Studio's "Debug / Attach to Process". You cannot do it before you have the process to debug running, but by the time you do it, the process will most likely pass fist two lines and enter your infinite wait state. The debugger will be attached, but the process will remain in the wait state. It makes no sense at all.

Basically, you have two ways for debugging: by attaching the debugger to a process or interactively. To attach a debugger, you can wait for just enough time in the thread you want to debug, not infinitely, and then, when the process is started, set a breakpoint after the sleep call. This is pretty boring, but sometimes it will help you.

You can also debug most of the problems on the process started interactively. For that purpose, you can use the fact that you can program slightly different behavior depending on how your process is started. I would not touch if (DEBUG). Instead, you can use identical process code, not depending on any precompiler settings. To tell interactive mode from the service, you can use the static variable System.Environment.UserInteractive. I developed a whole technique based on this flag; it allowed me to debug 99% of problems interactively, the way you asked about in your question. Of course, no thread sleep should be used.

For further ideas, please see my past answer: Configure Windows Service with UI[^].

—SA


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

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