我已經負責更新讀取文本文件的服務,並從文本文件的各個部分創建pdf文件並通過電子郵件發送PDF。我最近對服務進行了一些更改,並使用了.NET 4.0 Framework。在服務器上進行更新時,在安裝4.0 Framework之前,我可以移動文件併成功啓動服務 - 以前使用的是2.0。該服務運行,直到它到達試圖用pdf文件清理目錄的代碼。該服務在代碼嘗試刪除「正在被另一進程使用」的pdf文件時停止。代碼尋找這個異常,應該等待大約30秒,然後再次嘗試刪除。在本地,我通過測試Harness運行此服務,並循環直到文件可供刪除(平均需要大約5分鐘),但在服務器上,服務因應用程序事件日誌中發現的Unhandled IOException而停止。我不明白爲什麼它會繼續在本地處理,但不在服務器上。任何想法或其他信息將不勝感激。.Net運行時錯誤停止服務;未處理的IOException
以下是在事件日誌中的錯誤:
該過程由於未處理的異常終止。
異常信息信息:System.IO.IOException 堆棧: 在System.IO .__ Error.WinIOError(的Int32,System.String) 在System.Console.GetBufferInfo(布爾,布爾的ByRef) 在processorName。在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean)System.Threading.ThreadHelper.ThreadStart_Context(System.Object) 上FileProcessingThread.CleanUpDirectory(System.Object) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object) at Sys tem.Threading.ThreadHelper.ThreadStart(System.Object的)
此外,在事件日誌中顯示該錯誤,以及:
的EventType clr20r3,P1 myServiceName.exe,P2 1.0.1.0,P3 4db6e85d ,P4 mscorlib,P5 4.0.0.0,P6 4d53693b,P7 3dab,P8 23b,P9 system.io.ioexception,P10 NIL。
這是應該處理異常,等待並重試刪除文件的代碼。
while (!isDone)
{
bool myRetVal = true;
string myErrorMsg = String.Empty;
string myFile = String.Empty;
//give it time to finish processing
Thread.Sleep(30 * 1000);
//
//delete Files in folder
foreach (string file in Directory.GetFiles(myDirectory, "*.PDF"))
{ //delete all the *.PDF files
try
{
File.Delete(file);
}
catch (Exception ex)
{
myErrorMsg = "...\r\n" + ex.Message;
m_Log.Writeline("Unable to delete "+ file + ". MESSAGE:"+ myErrorMsg,3);
myFile = file;
myRetVal = false;
}
}
//
//now move the in-progress File to the processed directory
if (myRetVal)
{
foreach (string file in Directory.GetFiles(myDirectory, "*.InProgress"))
{
try
{
if (File.Exists(m_ConfigFile.ProcessedFolderName + Path.GetFileNameWithoutExtension(file) + ".done"))
{
File.Delete(file);
}
else
{
File.Move(file, m_ConfigFile.ProcessedFolderName + Path.GetFileNameWithoutExtension(file) + ".done");
}
}
catch (Exception ex)
{
if (ex.Message.Contains("file already exists"))
{
}
else
{
myErrorMsg = "...\r\n" + ex.Message;
myFile = file;
myRetVal = false;
}
}
}
}
//
//if empty, delete the SendMailFolder subfolder
if (myRetVal)
{
try
{
if (Directory.GetFiles(myDirectory, "*.*").Length == 0) Directory.Delete(myDirectory);
}
catch (Exception ex)
{
myErrorMsg = "...\r\n" + ex.Message;
myFile = myDirectory;
myRetVal = false;
}
}
if (Console.CursorLeft > 0) Console.WriteLine("\r\n");
if (myRetVal)
{
Console.WriteLine(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder...Done");
isDone = true;
}
else
{
if (myErrorMsg.Contains("is being used by another process."))
{
myErrorMsg = " is still in use.";
Console.WriteLine(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder..." + Path.GetFileName(myFile) + myErrorMsg);
}
else
{
Console.WriteLine(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder..." + Path.GetFileName(myFile) + myErrorMsg);
m_Log.Writeline(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder..." + Path.GetFileName(myFile) + myErrorMsg, 3);
isDone = true;
}
}
}
這是否導致錯誤?我不明白爲什麼它是用控制檯寫的東西,我不認爲它是必要的,因爲一些日誌記錄服務已經寫入日誌文件。 – JennyCB 2011-05-05 18:22:11
+1。請注意異常跟蹤:'在System.Console.GetBufferInfo(布爾值,布爾值ByRef)''這是使用控制檯導致問題,而不是與文件交互。我會刪除或改變調用到'Console.CursorLeft'和'Console.WriteLine' – CodingWithSpike 2011-05-05 18:35:48
謝謝!我會嘗試。 – JennyCB 2011-05-05 18:57:50