2015-05-21 201 views
0

當我嘗試啓動Windows服務後安裝使用NSSM則表明了我下面的錯誤Windows服務啓動問題

Window Service Error

添加的代碼

#region Private Members 

private Timer timer1 = null; 

#endregion 

#region Constructor 

public Service1() 
{ 
    InitializeComponent(); 
} 

#endregion 

#region Methods 
/// <summary> 
/// This function run on Windows service Start event 
/// </summary> 
/// <param name="args">args</param> 
protected override void OnStart(string[] args) 
{ 
    try 
    { 
     timer1 = new Timer(); 
     this.timer1.Interval = 60000;   //1 Minute=60000, 2 Minute=120000,5 Minute=300000    
     this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick); 
     timer1.Enabled = true; 
     Library.WriteErrorLog(" ============================ "); 
     Library.WriteErrorLog(" Windows Service Started. "); 
    } 
    catch (Exception ex) 
    { 
     Library.WriteErrorLog(" WCF Service Error Given In OnStart() Is : " + ex.Message); 
    } 
} 
/// <summary> 
/// OnStop event will hit when Windows service Stop 
/// </summary> 
protected override void OnStop() 
{ 
    timer1.Enabled = false; 
    Library.WriteErrorLog(" Windows Service Stopped. "); 

} 
/// <summary> 
/// timer1_Tick event will call in every Timer Interval as well Window Service Started 
/// </summary> 
/// <param name="state"></param> 
private void timer1_Tick(object sender, ElapsedEventArgs e) 
{ 
    Library.WriteErrorLog("---------------------------------"); 
    Library.WriteErrorLog(" Windows Service Timer Ticked and Doing His Job Succcessfully. "); 
    Library.WriteErrorLog(" Windows Service Timer Call After 1 Minute. "); 
    Library.WriteErrorLog(" WCF Service Call Started. "); 
    TimerCalled(); 
} 

/// <summary> 
/// This function will called in 'x' time interval. common function will call from OnElapsedTime event 
/// </summary> 
/// <param name="b"></param> 
private void TimerCalled() 
{ 
    try 
    { 
     Library.WriteErrorLog(" WCF Service Call By Using Windows Service Timer. "); 

     DbChecker obj = new DbChecker(); 
     Boolean bl = obj.checkRecord(); 

     Library.WriteErrorLog(" Windows Service Timer Call End. "); 
     Library.WriteErrorLog("---------------------------------"); 
    } 
    catch (Exception ex) 
    { 
     Library.WriteErrorLog(" WCF Service Error Given In TimerCalled() Is : " + ex.Message); 
    } 
} 
#endregion 

當我調試我當時的解決方案,我沒有得到任何錯誤或警告...服務運行完美,但我想可能是一個Windows服務器錯誤

+0

這裏沒有足夠的信息來提供有意義的答案。你有沒有檢查過任何日誌或事件查看器?你的服務應該做什麼等等? – Tim

+0

服務是爲了將庫存訂單轉移到亞馬遜mws賬戶而開發的 – Pratik

回答

0

這可能是由於幾乎所有因素,具體取決於您編寫的代碼。我能想到的唯一理由是:(1)未處理的異常;或者甚至是一個允許服務過程結束的處理過程; (2)更像是一個啓動過程完成應用程序的服務:當它啓動時,它將執行一些處理,然後結束,從而產生該消息。

尋找代碼中這兩行的提示。希望能幫助到你!

+0

感謝Johnny的快速回復。 – Pratik

+0

我已經把上面的代碼放在你的答案 – Pratik

+0

你可以嘗試的一件事是,如果Library.WriteErrorLog由於任何原因失敗,那會給你你所看到的:沒有日誌和任何地方的幫助;考慮嘗試超簡單的日誌記錄,直到事情至少給你最小的提示。 – johnjps111