2017-02-15 28 views
0

我分析我的(已退休)共同worker`s Windows應用程序源(C#)如何調試或捕獲窗口服務異常

當我點擊應用服務啓動按鈕,該服務轉向的開始,但2-3秒後停止。所以,我檢查了登錄事件查看器,它有一些問題。

通過

System.Data.SqlClient.SqlException終止該進程。

所以我試圖找到原因,但我不知道我該怎麼做。

起初,我試圖用流程調試器在Visual Studio中,

但我前面提到的,過程只需2-3秒內停止,所以,它`不可能的...

如何我可以檢查錯誤或調試服務?

我有一個完整的來源。請有人幫助我。

回答

0

讓你喜歡的Program.cs

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    static void Main() 
    { 
    #if DEBUG 
     Service1 myService = new Service1(); 
     myService.OnDebug(); 
     System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); 
    #else 
     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[] 
     { 
      new Service1() 
     }; 
     ServiceBase.Run(ServicesToRun); 
    #endif 

    } 
} 

和你Service1.cs文件應該是這樣..

public Service1() 
{ 
    InitializeComponent(); 
} 

public void OnDebug() 
{ 
    OnStart(null); 
} 

protected override void OnStart(string[] args) 
{ 
    // your code to do something 
} 

protected override void OnStop() 
{ 
} 

現在的基礎上,從Visual Studio中的模式切換 「調試/發佈」 ,您的Program.cs文件將被啓用/禁用。如果它在調試中,則調試部分將被啓用,其他將被註釋/禁用,反之亦然。

0

您可以使用下面的代碼調試您的Web服務代碼。

靜態類節目 {

static void Main() 
    { 
     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[] 
     { 
      new DataTransfer() 
     }; 

     if (Environment.UserInteractive) 
     { 
      RunInteractive(ServicesToRun); 

     } 
     else 
     { 
      ServiceBase.Run(ServicesToRun); 
     } 

     //ServiceBase.Run(ServicesToRun); 
    } 


    static void RunInteractive(ServiceBase[] servicesToRun) 
    { 
     Console.WriteLine("Services running in interactive mode."); 
     Console.WriteLine(); 

     MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart", 
      BindingFlags.Instance | BindingFlags.NonPublic); 
     foreach (ServiceBase service in servicesToRun) 
     { 
      Console.Write("Starting {0}...", service.ServiceName); 
      onStartMethod.Invoke(service, new object[] { new string[] { } }); 
      Console.Write("Started"); 
     } 

     Console.WriteLine(); 
     Console.WriteLine(); 
     Console.WriteLine(
      "Press any key to stop the services and end the process..."); 
     Console.ReadKey(); 
     Console.WriteLine(); 

     MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop", 
      BindingFlags.Instance | BindingFlags.NonPublic); 
     foreach (ServiceBase service in servicesToRun) 
     { 
      Console.Write("Stopping {0}...", service.ServiceName); 
      onStopMethod.Invoke(service, null); 
      Console.WriteLine("Stopped"); 
     } 

     Console.WriteLine("All services stopped."); 
     // Keep the console alive for a second to allow the user to see the message. 
     Thread.Sleep(1000); 
    } 
} 
+0

我使用上面的代碼,它是偉大的工作......那麼今天我得到一個異常{「異常已通過調用的目標引發異常。」內部異常:{「指定的服務不存在作爲已安裝的服務」}服務Service1在計算機'。'上未找到。 – user3174075