我已经负责更新读取文本文件的服务,并从文本文件的各个部分创建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