2013-03-21 135 views
0

我已經寫了一個服務,它在我的電腦上工作得很好,但是當我將它移動到服務器上時,它應該運行,它隨時都會吐出System.IndexOutOfRangeException,寫文件就像它的假設一樣。我只是覺得奇怪,它只是在服務器上這樣做,我不知道他們如何以不同的方式對我的電腦工作,所以任何幫助,將不勝感激。Windows Server 2012 System.IndexOutOfRangeException

描述:由於未處理的異常,進程終止。 異常信息:System.IndexOutOfRangeException 堆棧: 在emlService.emlService.runProc() 在System.Threading.ThreadHelper.ThreadStart_Context(System.Object的) 在System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,系統.Threading.ContextCallback,System.Object,Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean) at System.Threading.ExecutionContext.Run(System .Threading.ExecutionContext,System.Threading.ContextCallback,System.Object) at System.Threading.ThreadHelper.ThreadStart()

代碼

public partial class emlService : ServiceBase 
{ 
    Boolean _isRunning; 

    public emlService() 
    { 
     InitializeComponent(); 
     if (!System.Diagnostics.EventLog.SourceExists("emlServiceSource")) 
     { 
      System.Diagnostics.EventLog.CreateEventSource(
       "emlServiceSource", "emlServiceLog"); 
     } 
     eventLog1.Source = "emlSerivceSource"; 
     eventLog1.Log = "emlServiceLog"; 
    } 

    protected override void OnStart(string[] args) 
    { 
     eventLog1.WriteEntry("In OnStart"); 
     Thread NewThread = new Thread(new ThreadStart(runProc)); 
     _isRunning = true; 
     //Creates bool to start thread loop 
     if (_isRunning) 
     { 
      NewThread.Start(); 
     } 
    } 

    protected override void OnStop() 
    { 
     eventLog1.WriteEntry("In OnStop"); 
     _isRunning = false;    
    } 

    protected override void OnContinue() 
    {    
    } 

    public void runProc() 
    {   

     //Directory of the drop location 
     string tempDrop = ConfigurationSettings.AppSettings["conf_drop"]; 
     DirectoryInfo drop = new DirectoryInfo(tempDrop); 

     //Directory of the pickup location 
     string tempPickup = ConfigurationSettings.AppSettings["conf_pickup"]; 
     string destpath = tempPickup; 

     //Inits ParserCommands and DropDirectory object 
     ParserCommands parserObject = new ParserCommands(); 

     //Inits array to hold number of messages in drop location 
     FileInfo[] listfiles; 

     //Inits CFG 
     string conf_mailsender = ConfigurationSettings.AppSettings["conf_mailsender"]; 
     string conf_rcpt = ConfigurationSettings.AppSettings["conf_rcpt"]; 
     string conf_username_and_password = ConfigurationSettings.AppSettings["conf_username_and_password"]; 
     string conf_sender = ConfigurationSettings.AppSettings["conf_sender"]; 
     string conf_raport = ConfigurationSettings.AppSettings["conf_raport"]; 

     //Loop that never ends 
     while (true) 
     { 
      //Reduces load on machine 
      Thread.Sleep(1000); 

      //Checks if there is a message waiting to be processed and begins processing if so 
      listfiles = drop.GetFiles(); 
      if (listfiles.Length >= 1) 
      { 
       for (int j = 0; j <= (listfiles.Length - 1); j++) 
       { 
        //Gives it time to breathe 
        Thread.Sleep(250); 

        try 
        { 
         //Gets each line of the original .eml into a string array 
         var lines = File.ReadAllLines(listfiles[j].FullName); 
         string[] linestring = lines.Select(c => c.ToString()).ToArray(); 

         //Seperates start of email from the rest and decode parameter_content 
         string parameter_to = parserObject.getReciever(linestring[12]); 
         string parameter_content = parserObject.DecodeFrom64(linestring[17]); 

         //Creates string ready for base64 encode 
         string encode = "from=" + conf_sender + "&to=" + parameter_to + "&raport=" + conf_raport + "&message=" + parameter_content; 

         //Opens up steam and writer in the new dest, creates new .eml file 
         using (FileStream fs = new FileStream(destpath + listfiles[j].Name, FileMode.CreateNew)) 
         using (StreamWriter writer = new StreamWriter(fs)) 
         { 

          //Writes all .eml content into buffer 
          writer.WriteLine("x-sender: " + conf_mailsender); 
          writer.WriteLine("x-receiver: " + conf_rcpt); 
          writer.WriteLine(linestring[2]); 
          writer.WriteLine(linestring[3]); 
          writer.WriteLine(linestring[4]); 
          writer.WriteLine(linestring[5]); 
          writer.WriteLine(linestring[6]); 
          writer.WriteLine(linestring[7]); 
          writer.WriteLine(linestring[8]); 
          writer.WriteLine("From: " + conf_mailsender); 
          writer.WriteLine(linestring[10]); 
          writer.WriteLine("Reply-To: " + conf_mailsender); 
          writer.WriteLine("To: " + conf_rcpt); 
          writer.WriteLine("Subject: " + conf_username_and_password); 
          writer.WriteLine("Return-Path: " + conf_mailsender); 
          writer.WriteLine(linestring[15]); 
          writer.WriteLine(); 

          //Writes encoded string into buffer 
          writer.WriteLine(parserObject.EncodeTo64(encode)); 

          //Writes buffer into .eml file 
          writer.Flush(); 
         } 

         lines = null; 
        } 
         catch (System.IO.IOException e) 
        { 
         Console.WriteLine("no"); 

        } 

        //Deletes the file 
        File.Delete(listfiles[j].FullName); 
       } 

       //Sets the number of files needing sent to 0 
       listfiles = null; 
      } 
     } 
    } 
} 

回答

3

據我所知,你是不是檢查linestring長度的任何地方。我不能肯定地說,因爲你沒有提供足夠的信息,但我會認爲linestring.Length小於17,導致parserObject.DecodeFrom64(linestring[17]);拋出異常。這可能是因爲linestring.Length小於12,並且它是在此之前的行。

要解決這個問題,您應該檢查linestring的長度並適當處理結果。

string[] linestring = lines.Select(c => c.ToString()).ToArray(); 
if(linestring.Length <= 17) 
{ 
    //handle malformed file 
} 
else 
{ 
    //complete the processing 
} 

無關你的問題,但我很好奇,你怎麼想的lines.Select(c => c.ToString()效果。 File.ReadAllLines()已經返回一個字符串數組,所以`Select(c => c.ToString())是多餘的。

+0

我以另一種方式解決了這個問題,但我一定會將此代碼添加到我的服務中,謝謝!至於ReadAllLines,我不知道它是這樣工作的,我只是終於得到了服務的工作,並將在未來幾天內優化它,我一定會在那裏做出改變,謝謝! – user345453 2013-03-22 00:06:24

2

在你讀的一行代碼中,linestring並不是你期望的長度。

你確定這永遠不會失敗嗎?

linestring[17] 

如果您的文件中有空行,該怎麼辦?

+0

啊謝謝,這促使我看看這些文件,他們實際上是腐敗的,謝謝! – user345453 2013-03-22 00:05:22