NHibernate: The collection has been modified; enumeration operation may not be performed

I am currently struggling with this "The collection has been modified, the enumeration operation may not complete ."

I searched for this error message, and all this has to do with the foreach statement. I have some foreach statements, but they just represent data. I did not use delete or add inside the foreach statement.

NOTE:

  • An error occurs randomly (about 4-5 times a day).
  • The application is an MVC website.
  • About 5 users work in this application (about 150 orders per day). Can some other users modify the collection and then this error?
  • I have a log4net setting and the settings can be found here
  • Make sure that the controller does not have a constructor without parameters. I have a parameterless public constructor in the AdminProductController

Does anyone know why this is happening and how to solve this problem?

A friend (Oscar) noted that

"Theory: Perhaps the problem is that your configuration and factory session is initialized at the first request after the application is restarted. If the second request arrives before the first request is completed, it may also try to initialize and then initiating this problem somehow."

Many thanks.

Daoming

Here is the error message:

System.InvalidOperationException ; . System.InvalidOperationException: "WebController.Controllers.Admin.AdminProductController". , . --- > System.Reflection.TargetInvocationException: . --- > NHibernate.MappingException: . DomainModel.Entities.Mappings.OrderProductVariant.hbm.xml --- > System.InvalidOperationException: ; .  System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()   System.Xml.Schema.XmlSchemaSet.AddSchemaToSet( XmlSchema)   System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, XmlSchema)   System.Xml.Schema.XmlSchemaSet.Add( XmlSchema)   NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name)   NHibernate.Cfg.Configuration.AddInputStream( xmlInputStream, )  --- ---   NHibernate.Cfg.Configuration.LogAndThrow( )   NHibernate.Cfg.Configuration.AddInputStream( xmlInputStream, )   NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)   NHibernate.Cfg.Configuration.AddAssembly( )   DomainModel.RepositoryBase..ctor()   WebController.Controllers._baseController..ctor()   WebController.Controllers.Admin.AdminProductController..ctor()   System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)  --- ---   System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)   System.Activator.CreateInstance( , Boolean nonPublic)   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)  --- ---   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController & controller, IControllerFactory & factory)   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback, )   System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   System.Web.HttpApplication.ExecuteStep( IExecutionStep, )

+5
1

. factory . , , lock . , Wintellect PowerThreading:

using (_lock.WaitToRead())
{
    if (Factory != null) return Factory;
}
using (_lock.WaitToWrite())
{
    if (Factory != null) return Factory;
    Factory = ConfigureFactory();
    return Factory;
}

lock , :

class NestedSessionManager
{
    internal static SessionManager _sessionManager;
    private static readonly object _syncRoot = new object();

    internal static SessionManager sessionManager
    {
        get
        {
            if (_sessionManager != null) return _sessionManager;
            lock (_syncRoot)
            {
                if (_sessionManager != null) return _sessionManager;
                _sessionManager = new SessionManager();
                return _sessionManager;
            }
        }
    }
}
+4

All Articles