C# 如何调试或捕获windows服务异常

C# 如何调试或捕获windows服务异常,c#,debugging,exception,service,visual-studio-debugging,C#,Debugging,Exception,Service,Visual Studio Debugging,我正在分析我(退休)同事的windows应用程序源代码(C) 当我在应用程序中单击服务启动按钮时,该服务启动,但2-3秒后停止。所以,我在事件查看器中检查了日志,它有一些问题 进程被终止 System.Data.SqlClient.SqlException 所以我试着找出原因,但我不知道我该怎么做 起初,我尝试在Visual studio中使用进程调试器 但我之前提到过,这个过程只在2-3秒内停止,所以,这是不可能的 如何检查错误或调试服务 我有一个完整的来源。请有人帮帮我。让你喜欢Progra

我正在分析我(退休)同事的windows应用程序源代码(C)

当我在应用程序中单击服务启动按钮时,该服务启动,但2-3秒后停止。所以,我在事件查看器中检查了日志,它有一些问题

进程被终止

System.Data.SqlClient.SqlException

所以我试着找出原因,但我不知道我该怎么做

起初,我尝试在Visual studio中使用进程调试器

但我之前提到过,这个过程只在2-3秒内停止,所以,这是不可能的

如何检查错误或调试服务


我有一个完整的来源。请有人帮帮我。

让你喜欢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

    }
}

现在,基于Visual Studio中的模式开关“Debug/Release”,您的Program.cs文件将被启用/禁用。如果它在调试中,那么调试部分将被启用,其他部分将被注释/禁用,反之亦然。

您可以使用下面的代码调试web服务代码

静态类程序 {


我正在使用上面的代码,它工作得很好…然后今天我得到一个异常{“调用目标引发了异常。”内部异常:{“指定的服务不作为已安装的服务存在”}计算机上找不到服务Service1“。
   public Service1()
{
    InitializeComponent();
}

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

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

protected override void OnStop()
{
}
    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);
    }
}