Search code examples
asp.net-mvciiscontrollerlimit

Why does loop stop after being called 63 times? It should run to the end but stops for 90 mins after 63 calls


I have an MVC application and I am using it to process a list folder of XML files, I have the following code:

Function Backlog598()

    'Dim dir As New DirectoryInfo(Server.MapPath("~/598/"))
    Dim fileArray As FileInfo() = dir.GetFiles()
    Dim file As FileInfo
    Dim fileCounter = 0

    For Each file In fileArray

        fileCounter += 1
        Dim i As Integer
        Dim j As Integer

        'Declare Variables for Entity Creation Values
        Dim Var1 = ""
        Dim Var2 = ""
        Dim Var3 = ""
        Dim Var4 = ""
        Dim Var5 = ""
        Dim Var6 = ""
        Dim Var7 = ""

        'Declare Variable for the Message class
        Dim Message = New Message

        'Create a FileStream from the file saved in the MessagesFiles folder
        Dim fs As New FileStream(Server.MapPath("~/598/" + file.Name), FileMode.Open, FileAccess.Read)
        xmlDoc.Load(fs)
        fs.Close()
        xmlNodeList = xmlDoc.GetElementsByTagName("ieXMLDocument")

        'Process the Header Message
        For i = 0 To xmlNodeList.Count - 1
            For j = 0 To xmlNodeList(i).ChildNodes(0).Attributes.Count - 1
                'Set the relevant variable according to the header attributes
                Select Case xmlNodeList(i).ChildNodes(0).Attributes(j).Name
                    Case "Var1" : Var1 = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                    Case "Var2" : Var2 = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                    Case "Var3" : Var3 = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                    Case "Var4" : Var4 = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                    Case "Var5" : Var5 = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                    Case "Var6" : Var6 = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                End Select
            Next
        Next

        'Set the MessageHeader attributes
        marketMessage.CreatedOn = Date.Today
        marketMessage.Var4 = DateTime.Parse(Var4)
        marketMessage.Var2 = Var2
        marketMessage.Var3 = Var3
        marketMessage.Var6 = Var6
        marketMessage.Var1 = Var1
        marketMessage.Var5 = Var5
        marketMessage.fileName = file.Name

        'Add Message class to database context
        db.Messages.Add(Message)
        Try
            db.SaveChanges()
        Catch ex As DbEntityValidationException
            System.Diagnostics.EventLog.WriteEntry("Application", "There were validations errors while attempting to save a Message for the xml file named : " + file.Name, EventLogEntryType.Error)
            For Each validationErrors In ex.EntityValidationErrors
                For Each validationError In validationErrors.ValidationErrors
                    System.Diagnostics.EventLog.WriteEntry("Application", "Message Validation Error : " + validationError.ErrorMessage, EventLogEntryType.Error)
                Next
            Next
        End Try
        Process598(xmlDoc, Message)
        System.IO.File.Delete(Server.MapPath("~/598/" + file.Name)

        If fileCounter = 5 Then
            Return RedirectToAction("Backlog598")
        End If
    Next
    Return Nothing
End Function

This code hits the controller and iterates through the folder after processing 5 files and deleting each one as they are processed the controller then is recalled as if I leave this running continually it gets very slow due to the amount of reads and so I have decided to do it like this.

This is working so far that it process 63 x 5 records for a total of 315 records but then the controller does not get hit. If I leave this running overnight then it will process another set of records. If I have 50 records per save which I have done for other smaller files then it processes 3150 before stopping.

Would this be some sort of iis or browser limit that is stopping from running through all the files?


Solution

  • Google Chrome redirect loop error was causing the problem as it was detecting that this was an infinite loop