我有2個問題:調試和測試Windows服務
我創建一個服務器和進口每天每天一次在凌晨3點GMT,將導出數據庫到另一臺服務器Windows服務。我已經安裝了我的服務並按照以下步驟操作:https://msdn.microsoft.com/en-us/library/sd8zc8ha(v=vs.110).aspx。然而,我已經在服務的代碼中嘗試過了,它不會允許我附加到該過程,也嘗試在空白的視覺工作室頁面中執行它(同時作爲管理員),不能和問題是當我運行我的服務停止出於某種原因,所以如果可能的話,我需要一些關於如何測試和調試以確保服務正在做我想做的事情的想法。
我用我的事件查看器告訴我一些錯誤,但這似乎與時間,但是我收到一個錯誤,是相當昂貴:
服務無法啓動。 System.ArgumentException:在寫入事件日誌之前未設置Source屬性。 在System.Diagnostics.EventLogInternal.WriteEntry(字符串消息,EventLogEntryType類型,事件ID的Int32,Int16類型類別,字節[] RAWDATA) 在System.Diagnostics.EventLog.WriteEntry(字符串消息) 在DBBackUpService.BackUpService.OnStart(字符串[ ]參數)在C:\開發\項目\ DBBackUpService \ DBBackUpService \ BackUpService.cs:線27 在System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(對象狀態)
其中誤差開始的代碼是在OnStart方法開始時:
System.Timers.Timer _timer = null;
bool backUpRunning = false;
public BackUpService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
BackUpLog.WriteEntry("Database Backup has begun.");
_timer = new System.Timers.Timer { Interval = 3600000 };
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
Backup();
}
protected override void OnStop()
{
_timer.Stop();
BackUpLog.WriteEntry("Database Backup has finished.");
}
public void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimeSpan start = new TimeSpan(3, 0, 0);
TimeSpan end = new TimeSpan(5, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
BackUpLog.WriteEntry("Checking if it's time to run the archive service and checking if the service is already running");
if (backUpRunning)
BackUpLog.WriteEntry("Service is already running");
if ((now > start) && (now < end) && !backUpRunning)
{
BackUpLog.WriteEntry("Service is not already running and the time of day is valid to run the service.");
try
{
backUpRunning = true;
BackUpLog.WriteEntry("Initialising archive service");
}
catch (Exception ex)
{
logArchiveServiceError("The archive service failed. It will return to step 1 and try again: " + ex.Message);
}
finally
{
backUpRunning = false;
}
}
}
private void logArchiveServiceError(string errorMessage)
{
BackUpLog.WriteEntry(errorMessage, EventLogEntryType.Error);
//Emailer.SendErrorAlertEmail("Archive Service Error", errorMessage, null, null);
}
private void logArchiveServiceInfo(string info)
{
BackUpLog.WriteEntry(info, EventLogEntryType.Information);
}
要添加:此服務已被創建爲將數據從一個數據庫導出到另一個。
爲什麼不修復第27行的異常,以便查看事件? –
那麼這是更容易說,然後在我的情況下完成,因爲我是新的創建Windows服務我害怕,所以我不知道如何解決它。 –
如果您提供代碼,它會更容易幫助。 –