locked
AjaxControlToolkit error, Collection was modified; enumeration operation may not execute RRS feed

  • Question

  • User113076750 posted

    We have a production web site that we recently upgraded to the July 2013 release of the Ajax Control Toolkit. Since then, we're seeing an unhandled exception in the application event log. There doesn't seem to be a pattern to the error, and we only see it about once a day even though we have hundreds of active users. We don't believe we've made any other changes that are related to this problem. It doesn't seem to be related to any other events, like recycling or time of day.

    Is anyone else seeing this error?

    Any idea how to troubleshoot this one?

    Thanks for any ideas!

    Event ID: 1309

    Event code: 3005

    Event message: An unhandled exception has occurred.

    Exception type: InvalidOperationException

    Exception message: Collection was modified; enumeration operation may not execute.

    The full text of the event is shown below:

    Source: ASP.NET 4.0.30319.0
    Event ID: 1309
    
    Event code: 3005 
    Event message: An unhandled exception has occurred. 
    Event time: 8/27/2013 4:25:11 PM 
    Event time (UTC): 8/27/2013 8:25:11 PM 
    Event ID: b471019e51c94fc8b835f20803b11d23 
    Event sequence: 8523 
    Event occurrence: 19 
    Event detail code: 0 
     
    Application information: 
        Application domain: /LM/W3SVC/2/ROOT-1-130221037256083188 
        Trust level: Full 
        Application Virtual Path: / 
        Application Path: C:\Users\Administrator\Documents\AdaCareWeb\ 
        Machine name: H-ADACARE 
     
    Process information: 
        Process ID: 5784 
        Process name: w3wp.exe 
        Account name: NT AUTHORITY\NETWORK SERVICE 
     
    Exception information: 
        Exception type: InvalidOperationException 
        Exception message: Collection was modified; enumeration operation may not execute.
       at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
       at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetScriptReferences(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContent(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContentHash(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManager.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    
     
     
    Request information: 
        Request URL: https://my.adacare.com:443/Login.aspx?ReturnUrl=/ 
        Request path: /Login.aspx 
        User host address: 96.10.192.232 
        User:  
        Is authenticated: False 
        Authentication Type:  
        Thread account name: NT AUTHORITY\NETWORK SERVICE 
     
    Thread information: 
        Thread ID: 40 
        Thread account name: NT AUTHORITY\NETWORK SERVICE 
        Is impersonating: False 
        Stack trace:    at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
       at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetScriptReferences(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContent(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContentHash(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManager.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
     
     
    Custom event details: 
    



    Tuesday, August 27, 2013 9:16 PM

All replies

  • User-417640953 posted

      at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetScriptReferences(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContent(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContentHash(HttpContextBase context, String[] bundles)
       at AjaxControlToolkit.ToolkitScriptManager.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    
    

    Hello,

    I’m glad to see you post the issue to asp.net forum. Based on the exception information you provided, I Think the

    exception is caused by Control Bundles in a file located in AjaxControlToolkit.config which at the root of your application.

    For this issue, please see the comment in the following link, where StephenWalther has provided entire description and solution

    for a similar exception with you. Please try the method mentioned in it.

        http://stephenwalther.com/archive/2013/07/25/july-2013-release-of-the-ajax-control-toolkit

    If I misunderstand your issue, please do not hesitate to feedback.

     

    Best regards!

    Thursday, August 29, 2013 4:54 AM
  • User113076750 posted

    Actually, I'm not using bundles, so I don't have the AjaxControlToolkit.config file in my solution. About all we did was upgrade to the July 2013 release, with no other Ajax-related changes.

    Also, I'm not sure which comment in the blog you were referring me to. The error I'm seeing is "Collection was modified," not a null reference. Plus, it's an error that only appears about once a day on a busy web site.

    This problem has the feel of an Ajax bug, but I don't want to jump to that conclusion. Any ideas?

    Thursday, August 29, 2013 11:41 AM
  • User-673461378 posted

    Hi,

    I'm getting the exact same Exception, so I tried adding a bundle (with only the controls I am using) to see if this would make any difference.

    At first I thought it had worked, but that turned out not to be the case, the errors are still coming.

    I guess the next stage is to download the source code and try and see if I can figure out what it causing it, but the problem is that I've not had the problem myself, so could be a bit tricky.

    However, I am in agreement that this would seem like a bug, given that it only appeared once the July 2013 release was installed.

    Wednesday, September 4, 2013 4:57 AM
  • User-2027431084 posted

    I 'm getting the same Exception.   But I have never had the problem either... Any help would be appreciated.
    I am not using the Bundles.

     

    InnerException: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
    at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
    at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate) at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetScriptReferences(HttpContextBase context, String[] bundles)
    at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContent(HttpContextBase context, String[] bundles)
    at AjaxControlToolkit.ToolkitScriptManagerCombiner.GetCombinedScriptContentHash(HttpContextBase context, String[] bundles)
    at AjaxControlToolkit.ToolkitScriptManager.OnLoad(EventArgs e)
    at System.Web.UI.Control.LoadRecursive()
    at System.Web.UI.Control.LoadRecursive()
    at System.Web.UI.Control.LoadRecursive()
    at System.Web.UI.Control.LoadRecursive()
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Wednesday, September 4, 2013 6:03 AM
  • User1475947409 posted

    I'm also getting the same exception after switching to ToolkitScriptManager after the last release. I then tried adding the bundles config but continue to get the error, but only a couple of times a day and on more than one page, though they share the same MasterPage.

    Wednesday, September 4, 2013 12:36 PM
  • User821508642 posted

    Same issue here as well...Anyone solve it?

     

    https://ajaxcontroltoolkit.codeplex.com/workitem/27516

     

    Thursday, September 5, 2013 4:37 PM
  • User1475947409 posted

    From looking at the source code I think the problem is that ToolkitScriptManager has a static reference to ToolkitScriptManagerCombiner, but some of the methods in ToolkitScriptCombiner, including GetScriptReferences, are not thread-safe.

    Monday, September 9, 2013 1:36 AM
  • User113076750 posted

    Thanks for looking into the code, Stephen. The problem behaves like a threading issue, too, in that it only happens intermittently. I don't spend much time on this forum -- what's the best way to have this looked at for a possible bug fix?

    Monday, September 9, 2013 8:24 AM
  • User-990297241 posted

    I think the best thing to do is to follow the link on nuronce's post above and vote on the issue and/or post a comment on codeplex.

    Monday, September 9, 2013 10:17 AM
  • User-872827780 posted

    I have been getting this error recently and looked into ToolkitScriptManagerCombiner as you mentioned. I did see that this code may be causing the issue:

     scriptReferences = scriptReferences.Distinct().ToList();
                if (addedScriptReferences != null) {
                    foreach (var script in addedScriptReferences) {
                        scriptReferences.Add(script);
                    }
                }
    
                if (removedScriptReferences != null) {
                    foreach (var script in removedScriptReferences) {
                        var scriptToRemove = scriptReferences.FirstOrDefault(s => s.Name.Equals(script.Name) && s.Assembly.Equals(script.Assembly));
                        if (scriptToRemove != null)
                            scriptReferences.Remove(scriptToRemove);
                    }
                }

    It may be that if addedScriptReferences or removedScriptReferences changes then the underlying list would be modified. I do not see any harm in cloning the lists so they cannot be modified and using the clones instead.

    Something like this:

     scriptReferences = scriptReferences.Distinct().ToList();
                if (addedScriptReferences != null) {
                    var addedScriptReferencesClone = addedScriptReferences.ToList();
                    foreach (var script in addedScriptReferencesClone) {
                        scriptReferences.Add(script);
                    }
                }
    
                if (removedScriptReferences != null) {
    		var removedScriptReferencesClone = removedScriptReferences.ToList();			
                    foreach (var script in removedScriptReferencesClone) {
                        var scriptToRemove = scriptReferences.FirstOrDefault(s => s.Name.Equals(script.Name) && s.Assembly.Equals(script.Assembly));
                        if (scriptToRemove != null)
                            scriptReferences.Remove(scriptToRemove);
                    }
                }

    FWIW

    Thursday, February 27, 2014 11:28 AM