簡體   English   中英

1053 windows服務沒有及時響應

[英]1053 windows service did not respond in timely fashion

我有一個在IIS中運行的Web應用程序,並通過托管在同一台機器上的WCF與Windows服務進行通信。 內部Windows服務執行各種作業,有時它非常忙於處理用戶請求。 在第一天,我遇到了服務開始的問題,因為它有些時間需要花費很多時間來啟動並最終拋出錯誤即服務超時。 為了解決這個問題,我將代碼移到另一個Thread而不是Main Thread。 一切正常,但現在我發現有時我收到服務停止的錯誤即

**1053 windows service did not respond in timely fashion**

我的服務OnStop()代碼:

protected override void OnStop()
        {
            // Stop the WCF service
            if (myServiceHost != null)
            {
                myServiceHost.Close();
                myServiceHost = null;
            }

            // Stop the scheduler

            if (myScheduleManager != null)
            {
                myScheduleManager.Stop();
            }

            // Update the registry value
            Logger.GetInstance().LogEvent(this.GetType().Name, LogLevel.INFO, "Updating registry...");
            Settings.GetInstance().LastStopTime = DateTime.Now.ToString();

            Logger.GetInstance().LogEvent(this.GetType().Name, LogLevel.INFO, "Service stopped.");
        }

通常在服務太忙時發生。 但最終它在30-40分鍾后停止。 我知道這是由於Windows服務中的超時問題而發生的,因為默認時間非常短,並將其視為超時。

我的問題是什么是避免這種瓶頸的最佳實踐意味着我應該將我的Stop相關任務移動到另一個Thread並執行異步調用以釋放該Thread中的資源。 但是如果在發布資源期間,用戶將啟動服務會發生什么?

只是猜測,但也許它將有助於追查問題:

你有這個myScheduleManager.Stop() ,(我猜)等待進程完成。 這很好,但Windows只給你30秒執行ServiceBase.OnStop()

如果您在此處進行異步處理,則可以使用.NET-Framework中的CancellationTokens實現CancellationPattern。

您可以通過簡單的方法識別在myScheduleManager構建布爾類型IsRunning並在OnStop()期間確定

if (myScheduleManager.IsRunning)
    myScheduleManager.Stop();

但是,您有不同的線程完成可能超過30秒的過程,但您也可以通過應用中止您的過程

if (myScheduleManager.IsRunning && !_workerThread.Join(TimeSpan.FromSeconds(30))
    _workerThread.Abort 

myScheduleManager.Stop();

但是,我沒有看到完整的代碼片段,所以這是我對你的場景的猜測。

只是猜測但它可能有所幫助:確保將解決方案配置設置為Release而不是Debug。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM