2017-02-15 67 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