C# 如何调试 .NET Windows Service OnStart 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1196531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to debug the .NET Windows Service OnStart method?
提问by Nathan
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.
我有用 .NET 编写的代码,只有在作为 Windows 服务安装时才会失败。失败甚至不允许服务启动。我不知道如何进入 OnStart 方法。
How to: Debug Windows Service Applicationsgives a tantalizing clue:
如何:调试 Windows 服务应用程序提供了一个诱人的线索:
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.
附加到服务的进程允许您调试大部分但不是所有服务的代码;例如,因为服务已经启动,所以不能通过这种方式调试服务的OnStart方法中的代码,或者用于加载服务的Main方法中的代码。解决此问题的一种方法是在您的服务应用程序中创建一个临时的第二个服务,该服务仅用于帮助调试。您可以安装这两个服务,然后启动这个“虚拟”服务来加载服务进程。临时服务启动进程后,您可以使用 Visual Studio 中的“调试”菜单附加到服务进程。
However, I'm not clear how it is exactly that you are supposed to create the dummy service to load the service process.
但是,我不清楚您应该如何创建虚拟服务来加载服务进程。
采纳答案by palehorse
One thing you could do as a temporary workaround is to launch the debugger as the first line of code in the OnStart
作为临时解决方法,您可以做的一件事是将调试器作为 OnStart 中的第一行代码启动
System.Diagnostics.Debugger.Launch()
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.
这将提示您选择要使用的调试器。只需在 Visual Studio 中打开该解决方案,然后从列表中选择该实例即可。
回答by Alex Black
You can add a line of code like this:
您可以添加这样一行代码:
System.Diagnostics.Debugger.Break()
which will bring up a window prompting you to choose which debugger to use to debug, e.g. allowing you to attach with Visual Studio and step into the code.
这将打开一个窗口,提示您选择用于调试的调试器,例如,允许您附加 Visual Studio 并进入代码。
see:
看:
http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break.aspx
回答by Dror Helper
Try adding Debugger.Break inside the problematic method. When the service will start an exception will be thrown and widows should offer to debug it using visual studio.
尝试在有问题的方法中添加 Debugger.Break。当服务启动时,将抛出异常,寡妇应提供使用 Visual Studio 对其进行调试。
回答by HasaniH
I tend to add a method like this:
我倾向于添加这样的方法:
[Conditional("DEBUG")]
private void AttachDebugger()
{
Debugger.Break();
}
it will only get called on Debug builds of you project and it will pause execution and allow you to attach the debugger.
它只会在您项目的 Debug 版本上被调用,它会暂停执行并允许您附加调试器。
回答by Matt
I usually have a console app that pretends to be the SCM e.g. calls Start, Stop which I can then just F5 into for my main coding/debugging purposes, and use the Debugger.Break for debugging when the service has been installed and started via the SCM.
我通常有一个伪装成 SCM 的控制台应用程序,例如调用 Start、Stop,然后我可以 F5 进入我的主要编码/调试目的,并在服务安装和启动时使用 Debugger.Break 进行调试。单片机。
It means a bit more work to start with, I have a class lib that contains all the service code, with a class that exposes Start and Stop that the Windows Service class and the console app can both call.
这意味着要开始做更多的工作,我有一个包含所有服务代码的类库,其中一个类公开 Windows 服务类和控制台应用程序都可以调用的启动和停止。
Matt
马特
回答by Developer
It works just fine!
它工作得很好!
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
}
回答by Ryan Kohn
It's possible to set up a companion project to the Windows Service that runs as a console app, but accesses the service methods using Reflection. See here for details and an example: http://ryan.kohn.ca/articles/how-to-debug-a-windows-service-in-csharp-using-reflection/.
可以为作为控制台应用程序运行的 Windows 服务设置一个配套项目,但使用反射访问服务方法。有关详细信息和示例,请参见此处:http: //ryan.kohn.ca/articles/how-to-debug-a-windows-service-in-csharp-using-reflection/。
回答by Boris Hurinek
The options above did not appear to work on Windows 8.
上述选项似乎不适用于 Windows 8。
I have added Thread.Sleep(15000); into my OnStart() method and set a breakpoint on the next line of the code. This give me 15 seconds to attach VS debugger to my process after starting the service and allowed me to debug the OnStart() method nicely.
我添加了 Thread.Sleep(15000); 进入我的 OnStart() 方法并在代码的下一行设置断点。这给了我 15 秒的时间在启动服务后将 VS 调试器附加到我的进程,并允许我很好地调试 OnStart() 方法。
回答by Coach David
I know this is late but this is how we handle debuging Windows services
我知道这已经晚了,但这就是我们处理调试 Windows 服务的方式
First create a class which will act as the service.
首先创建一个作为服务的类。
Add the appropriate methods for starting, stopping, pausing, etc...
添加适当的启动、停止、暂停等方法......
Add a windows form to the service project.
向服务项目添加一个 Windows 窗体。
In the service code create the service class created above and make the calls needed to start and stop the service in the ServiceBase class
在服务代码中创建上面创建的服务类,并在 ServiceBase 类中进行启动和停止服务所需的调用
Open the Program.cs and add the following
打开 Program.cs 并添加以下内容
#if DEBUG
[STAThread]
#endif
static void Main()
{
try
{
#if DEBUG
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DebugForm());
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new YourWindowsService()
};
ServiceBase.Run(ServicesToRun);
#endif
}
catch (Exception e)
{
logger.Error(DateTime.Now.ToString() + " - " + e.Source + " - " + e.ToString() + "\r\n------------------------------------\r\n");
}
}
When you run in DEBUG mode the windows form will launch. Just remember to build in Release mode when finished. Of course the conditional compile variable can be anything you like. You could even create seperate projects so the debug form is its own project.
当您在调试模式下运行时,窗口窗体将启动。只需记住在完成后以发布模式构建。当然,条件编译变量可以是你喜欢的任何东西。您甚至可以创建单独的项目,因此调试表单是它自己的项目。
Hope this helps
希望这可以帮助
回答by Abhishek Srivastava
You can also try System.Diagnostics.Debugger.Launch()method. It helps in taking the debugger pointer to the specified location and you can then debug you code.
您还可以尝试System.Diagnostics.Debugger.Launch()方法。它有助于将调试器指针指向指定位置,然后您可以调试代码。
Before this step please install your service.exeusing the command line of Visual Studio command prompt - installutil projectservice.exe
在此步骤之前,请使用 Visual Studio 命令提示符的命令行安装 service.exe- installutil projectservice.exe
Then start your service from the Control Panel -> Administrative Tools -> Computer Management ->Service and Application -> Services -> Your Service Name
然后从控制面板 -> 管理工具 -> 计算机管理 -> 服务和应用程序 -> 服务 -> 您的服务名称启动您的服务