.Net开发应用

.Net项目开发、技术研究、应用技巧、心得体会

导航

调试 OnStart 方法

原文看:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtskdebuggingonstartmethod.asp


通过步进一个服务的实例,可以调试一个 Windows 服务的构造器。通过Windows 服务自己启动服务以及附加调试器到一个服务进程,你可以调试 Windows 服务。然而,调试 Windows 服务的 OnStart 方法,你就必须增加一些模拟服务的代码。更多信息,看 you ServiceBase.OnStart Method.

OnStart 方法中调试一个问题

  • 创建一个服务的模拟器(比如:控制台应用)来判断问题在哪里。

    例如,假定有这样一个 Visual C# 服务:

    public class ManagedWindowsService : System.ServiceProcess.ServiceBase 
    {
       //
       // designer and user generated methods and properties
       //
       public static int main(String[] args)
       {      
          ServiceBase.Run( new MangedWindowsService() );
       }
    }

    添加下面的代码行调试 OnStart方法:

    public static int main(String[] args)
    {      
       (new ManagedWindowsService()).OnStart(); // allows easy debugging of OnStart()
       ServiceBase.Run( new MangedWindowsService() );
    }

    服务将不能在这种模式下运行,但是你能调试 OnStart 方法,并且验证他的行为是否服务期望。

更多信息,看 Debugging Windows Service Applications.

posted on 2005-11-14 16:46  风也火  阅读(773)  评论(0)    收藏  举报