【问题标题】:How to restart a windows service after an unhandled exception occurs发生未处理的异常后如何重新启动 Windows 服务
【发布时间】:2017-11-29 00:02:52
【问题描述】:

我有一个控制台应用程序,它是一个 FTP 服务器。此控制台应用程序运行良好。现在,我想使用 Windows 服务运行这个 FTP 服务器。

我有一个未处理的异常捕获器,它捕获一个未处理的异常。出现此异常后,我想停止服务,销毁 FTP Server 的类,延迟 10 秒,然后重新启动服务。

以下是我的代码(如果没有未处理的异常,ftp 服务器和服务可以正常工作,但我想成功停止并重新启动服务。此代码可以正常停止服务但不会重新启动它)。有什么想法吗?

public partial class FTPService : ServiceBase
{
    private static FtpServer _ftpServer;
    public FTPService(string[] args)
    {
        InitializeComponent(); 

        string eventSourceName = "Ftp Server Events";

        eventLog1 = new System.Diagnostics.EventLog();

        string logName = "Ftp Server Log";

        if (args.Count() > 0)
        {
            eventSourceName = args[0];
        }

        if (args.Count() > 1)
        {
            logName = args[1];
        } 

        eventLog1 = new System.Diagnostics.EventLog();

        if (!System.Diagnostics.EventLog.SourceExists(eventSourceName))
        {
            System.Diagnostics.EventLog.CreateEventSource(eventSourceName, logName); 

        } 

        eventLog1.Source = eventSourceName; 
        eventLog1.Log = logName;

        if (!System.Diagnostics.EventLog.SourceExists("Ftp Server Events"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "Ftp Server Events", "Ftp Server Log");
        }

        this.ServiceName = "FTP Service";

        this.AutoLog = true;
    }

    public static void Main(string[] args)
    {
        ServiceBase.Run(new FTPService(new string[0]));
    }

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);

        var database = new Database(); // Gets database details as FTP server tals to database.
        var configurationManager = new ConfigurationManagerWrapper(); // Same as above

        _ftpServer = new FtpServer(new Assemblies.Ftp.FileSystem.StandardFileSystemClassFactory(database, configurationManager));
        _ftpServer.Start(); //Starts the service (FTP Server works fine if there is no handled exception)

        eventLog1.WriteEntry("Started");

        FtpServerMessageHandler.Message += MessageHandler_Message;

        AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

    }

    protected override void OnStop()
    {
        _ftpServer.Stop(); // This calls the destructor for FTP Server, to close any TCP Listening connections, etc
        base.OnStop(); // Here I stop the service itself.
        eventLog1.WriteEntry("Stopped");
        Thread.Sleep(10000);
    }

    protected void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) // unhandled exception handler
    {
        eventLog1.WriteEntry(e.ExceptionObject.ToString());
        Thread.Sleep(5000);
        OnStop(); // Calls onstop  which stops FTP Server and destroys previous objects of FTP server

        var serviceMgr = new ServiceController();
        serviceMgr.Start(); // Here I want to restart the service (it doesn't work)
    }


}

【问题讨论】:

  • 当您停止服务时,您的代码将停止运行。如果一个服务想要重新启动自己,它必须启动另一个进程来完成它。但是允许未处理的异常使程序崩溃,并配置服务管理器为您重新启动服务会更容易。
  • @Harry 谢谢伙计。我不能使用任何 ServiceController start() 方法来重新启动服务。或者,可以将服务设置为自动,以便在停止时自行重新启动?
  • 您可以使用 Start() 重新启动服务 - 但不能使用正在重新启动的服务中运行的代码。当服务停止时,代码 停止,因此 Start() 永远不会被调用 - 调用 base.OnStop() 就像调用 Environment.Exit() 一样,调用永远不会返回。将服务设置为自动也不能满足您的要求。

标签: c# windows-services unhandled-exception


【解决方案1】:

尝试以下操作:

services.msc -> 你的服务-> 属性 -> 恢复

为你设置属性

-> 保存 -> 你的服务 -> 重启

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 2010-12-18
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多