你如何调试Windows服务? [英] How do you debug a Windows Service?

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

问题描述

我看了关于这个专题的MSDN文章。引用:

I read the MSDN article on the topic. To quote:

由于服务必须从
服务
控制管理的环境中运行,而不是在Visual Studio中
,调试
服务并不像
调试其他Visual Studio
类型的应用程序一样简单。要调试服务,
你必须启动该服务,然后
安装调试器来运行它在
的过程。然后,您可以
调试使用的所有
的Visual Studio标准的调试功能
你的应用程序。

Because a service must be run from within the context of the Services Control Manager rather than from within Visual Studio, debugging a service is not as straightforward as debugging other Visual Studio application types. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug your application using all of the standard debugging functionality of Visual Studio.

现在我的问题是,我的服务无法在第一时间启动。首先,它崩溃,并说:

Now my problem is that my service fails to start in the first place. First it crashes, and says:

未处理的异常
(System.Runtime.InteropServices.COMException)
发生在MyServiceName.exe [3596])

An unhandled exception (System.Runtime.InteropServices.COMException) occurred in MyServiceName.exe[3596])

和建议我调试它(​​当我选择一个调试器实例瞬间崩溃)。然后它说

and suggests me to debug it (the debugger instance instantly crashes when I choose one). Then it says

无法在本地计算机上的服务名
服务。错误
1053年:服务没有$回应B $ b在一个
及时

Could not start the MyServiceName service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion

所以,我怎么能调查/调试我的服务将无法启动的原因是什么?事情是我创建了一个控制台应用程序,不正是服务做什么,它工作正常。 (我是说我抄袭了的OnStart ()方法的与主回路的内容主要)。

So, how can I investigate/debug the reason that my service won't start? The thing is I created a console application that does EXACTLY what the service does and it works fine. (I mean I just copied the OnStart() method's and the main loop's contents to main).

任何帮助将不胜感激。

本服务用C#编写大量使用互操作的。我使用VS2008

The Service is written in C# with heavy use of interop. I am using VS2008

推荐答案

您可以使用一个参数来让你的应用程序决定是否启动的服务或普通的应用程序(即这种情况下显示窗体或启动服务):

You could use a parameter to let your application decide whether to start as service or regular app (i.e. in this case show a Form or start the service):

static void Main(string[] args)
{
    if ((1 == args.Length) && ("-runAsApp" == args[0]))
    {
        Application.Run(new application_form());
    }
    else
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyService() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}

如果您通过参数

现在-runAsApp你可以正常调试应用程序 - 单片机不会通过这个参数,所以你也可以把它作为服务W / O更改任何代码(前提是你派生自 ServiceBase

编辑:

与Windows服务的另一个区别是身份(这可能是与互操作尤其重要) - 你想确保你下的应用程序模式以及服务模式相同的标识测试

The other difference with windows services is identity (this might be especially important with InterOp) - you want to make sure you are testing under the same identity in "app" mode as well as service mode.

这样做,你可以使用模拟在应用程序模式(我可以发布一个C#包装,如果有帮助,但是这可以很容易地用Google搜索)使用你的Windows服务将根据即通常本地服务或网络运行相同的身份。

To do so you can use impersonation (I can post a C# wrapper if it helps, but this can be easily googled) in app mode to use the same identity your windows service will be running under i.e. usually LocalService or NetworkService.

如果另一个身份是必需的,你可以添加设置在app.config,让您决定是否使用的凭据,如果是哪个用户冒充 - 作为应用程序运行时,这些设置将是积极的,但关闭对于Windows服务(因为该服务已经在所需的身份运行):

If another identity is required you can add settings to the app.config that allow you to decide whether to use credentials, and if so which user to impersonate - these settings would be active when running as app, but turned off for the windows service (since the service is already running under the desired identity):

  <appSettings>
    <add key="useCredentials" value="false"/>
    <add key="user" value="Foo"/>
    <add key="password" value="Bar"/>
  </appSettings>

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

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