0

I'm looping through the 10,000 items & exporting data to the excel.

If I limit loop to the 2k OR 3k items then there is no issue. everything works fine & data will get exported. but when loop goes for 10k items then its throwing thread aborted exception after 5k items.

In loop, I'm loading 3 xml documents. Exceptions also throwing at System.Xml.XmlDocument.LoadXml(String xml).

Below is my code for your Reference:

XmlDocument siteAdminXmlDocument = new XmlDocument();
XmlDocument engagementXmlDocument = new XmlDocument();
XmlDocument requestXmlDocument = new XmlDocument();

foreach (var request in provisioningRequests)
{
    //Load SiteAdmin XML
    siteAdminXmlDocument.LoadXml("<engagementAdministrators xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></engagementAdministrators>");
    string siteAdminXmlData = request.SiteAdminsXml;
    if (siteAdminXmlData != null)
    {
        siteAdminXmlDocument.LoadXml(siteAdminXmlData);
    }

    //Load Engagement XML
    engagementXmlDocument.LoadXml("SyncXml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></SyncXml>");
    string engagementXmlData = request.EngagementXml;
    if (engagementXmlData != null)
    {
        engagementXmlDocument.LoadXml(engagementXmlData);
    }

    //Load Request XML
    requestXmlDocument.LoadXml("<engagementRequest xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></engagementRequest>");
    string requestXmlData = request.RequestXml;
    if (requestXmlData != null)
    {
        requestXmlDocument.LoadXml(requestXmlData);
    }

    ////Here I'm getting data from xml nodes & writing to the Response//////
}

Error:

Thread was being aborted.                               
    at System.Threading.Monitor.Enter(Object obj)                               
   at System.Xml.XmlElementListListener.OnListChanged(Object sender, XmlNodeChangedEventArgs args)                                  
   at System.Xml.XmlNodeChangedEventHandler.Invoke(Object sender, XmlNodeChangedEventArgs e)                                    
   at System.Xml.XmlElement.AppendChildForLoad(XmlNode newChild, XmlDocument doc)                                   
   at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)                                 
   at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)                                   
   at System.Xml.XmlDocument.Load(XmlReader reader)                                 
   at System.Xml.XmlDocument.LoadXml(String xml)

NOTE: All 10,000 items are having same data. (i.e. those are 10k copies of single item in database table. i.e dummy data)

While googling I found some solutions on this but none of them are applicable in this case. Anyone knows why this happening? Any help will be appreciated.

SP Developer
  • 153
  • 2
  • 10
  • I assume you already looked at [this question](https://stackoverflow.com/questions/7629986/why-am-i-getting-thread-was-being-aborted-in-asp-net)? – Matthew Watson Sep 11 '19 at 09:35
  • The xml file is not valid. Verify xml with on-line checker or use VS menu : Project : Add New Item : Xml File. Paste xml into view. Error will appear in the error list like any compiler error. – jdweng Sep 11 '19 at 09:40
  • @jdweng If xml file is not valid then it should not work for single item as well. Actually code is working fine if there are 2k items in collection. – SP Developer Sep 11 '19 at 09:45
  • What if the 1000 xml file is bad and first 999 are good? – jdweng Sep 11 '19 at 09:52
  • Do you mean to load into the same document each time? If not, does moving the constructors inside the loop fix it? – Whelkaholism Sep 11 '19 at 09:54
  • @jdweng. Technically your question is right. But I'm having 10k copies of same item in table. So data, xml of all the 10k items is same. see my updated post. – SP Developer Sep 11 '19 at 09:54
  • But you are reading a different string each time. The string could have special characters that are causing an issue or something else. It must be the data in the file. – jdweng Sep 11 '19 at 10:06
  • @jdweng different strings but it contains same xmls in 10k item – SP Developer Sep 11 '19 at 11:41
  • Change you for loop to read the SAME file 10000 and see if you get the same error. – jdweng Sep 11 '19 at 13:28
  • @jdweng. Same error after 3k items. Its not related to valid/invalid xml as string contains same xml string in 10k items. – SP Developer Sep 12 '19 at 06:18
  • I have added answer for this. Thanks all for your comments. Appreciated ! – SP Developer Sep 12 '19 at 08:18

1 Answers1

0

Finally, I found out the cause & solution on for this issue.

In ASP.NET, Default execution timeout for any HTTP request is 110 seconds (90 seconds for .NET Framework 1.0 and 1.1). If request going beyond specified executionTimeout then it will get automatically aborted by ASP.NET.

So in my case, we were looping through more than 10k items & its htting default executionTimeout of 110 second then thread aborted automatically.

So, the solution is, Increase executionTimeout in web.config file of your application.

<httpRuntime executionTimeout = "HH:MM:SS"/>

OR

<httpRuntime executionTimeout = "Seconds"/>

OR

More reliable solution is, instead of increasing executionTimeout for entire application, You should increase it for specific page only on which you are doing your long running process. For that you can do it through code & no need to modify web.config. I set it for 1 hour(3600 Seconds)

Page.Server.ScriptTimeout = 3600;

Hope this help someone who having the same issue. Thanks.

SP Developer
  • 153
  • 2
  • 10
  • That only covers up the problem, it doesn't fix it nor is it reliable. ASP.NET request threads are *not* meant to handle long requests. There are proper ways to handle long-running jobs. For ASP.NET they are described in Scott Hanselman's [How to run Background Tasks in ASP.NET](https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx). ASP.NET Core introduced hosted services on top of that – Panagiotis Kanavos Sep 12 '19 at 09:08