2011-05-11 92 views
0

我已经创建了Windows服务我尝试调试,我使用在调试选项卡点击附加到进程选择Myservice.exe它不会经过中断点。Windows服务调试问题

在上启动事件服务,我写了下面的代码

 protected override void OnStart(string[] args) 
    { 

     Console.WriteLine("Press Enter to terminate ..."); 
    } 

请帮助我如何解决这个问题....

回答

4

使用以下method.On你的代码。这是迄今为止在服务库上设置断点的最简单的方法。

Debugger.Break();

protected override void OnStart(string[] args) 
    { 
     Debugger.Break(); 
     Console.WriteLine("Press Enter to terminate ..."); 
    } 
+0

感谢您的解决方案,它为我工作 – 2011-05-11 09:46:03

0

你也可以使用这样的提示附加在调试模式下的调试器:

#if DEBUG  
if (!Debugger.IsAttached) 
{ 
    Debugger.Launch(); 
}  
#endif 

你甚至可以在你的主要方法使用此代码,它将在调试模式下作为正常应用运行该服务:

public static void Main() 
{ 
    var service = new YourService(); 
#if DEBUG 
    service.Start(); 
    Console.ReadLine(); 
    service.Stop(); 
#else 
    var ServicesToRun = new System.ServiceProcess.ServiceBase[] { service }; 
    System.ServiceProcess.ServiceBase.Run(ServicesToRun); 
#endif 
}