Collection was modified; enumeration operation may not execute

833 views
Skip to first unread message

Daoming Yang

unread,
Apr 7, 2010, 5:23:44 AM4/7/10
to nhu...@googlegroups.com
Hi All, 

I'm currently struggling with this "Collection was modified; enumeration operation may not execute" issue. I have searched about this error message, and it's all related to the foreach statement. I do have the some foreach statements, but they are just simply representing the data. I did not using any remove or add inside the foreach statement.

NOTE: 
  1. The error randomly happens (about 4-5 times a day).
  2. The application is the MVC website.
  3. There are about 5 users operate this applications (about 150 orders a day). Could it be some another users modified the collection, and then occur this error?
  4. I have log4net setup and the settings can be found here: http://community.jboss.org/wiki/Configuringlog4netforusewithNHibernate
  5. Make sure that the controller has a parameterless public constructor  I do have parameterless public constructor in AdminProductController
Does anyone know why this happen and how to resolve this issue?


Many thanks.

Daoming



System.InvalidOperationExceptionCollection was modified; enumeration operation may not execute.

System.InvalidOperationException: An error occurred when trying to create a controller of type 'WebController.Controllers.Admin.AdminProductController'. Make sure that the controller has a parameterless public constructor. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.MappingException: Could not configure datastore from input stream DomainModel.Entities.Mappings.OrderProductVariant.hbm.xml ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
   at System.Xml.Schema.XmlSchemaSet.AddSchemaToSet(XmlSchema schema)
   at System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, XmlSchema schema)
   at System.Xml.Schema.XmlSchemaSet.Add(XmlSchema schema)
   at NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name)
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
   --- End of inner exception stack trace ---
   at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
   at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
   at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
   at DomainModel.RepositoryBase..ctor()
   at WebController.Controllers._baseController..ctor()
   at WebController.Controllers.Admin.AdminProductController..ctor()
   at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
   --- End of inner exception stack trace ---
   at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   --- End of inner exception stack trace ---
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Tuna Toksoz

unread,
Apr 7, 2010, 5:28:06 AM4/7/10
to nhu...@googlegroups.com
Seems to me that you are initializing NH Configuration (and also session factory) many times?

Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike




On Wed, Apr 7, 2010 at 12:23 PM, Daoming Yang <daomi...@googlemail.com> wrote:
   at WebController.Controllers._baseController..ctor()
   at WebController.Controllers.Admin.AdminProductController..ctor()

Daoming Yang

unread,
Apr 7, 2010, 5:40:40 AM4/7/10
to nhu...@googlegroups.com
I open the session for each request and close it at the end of the request.

Here is the code in the Global.asax.cs file.

Can anyone advise me?

Many thanks.

Daoming
protected void Application_BeginRequest(object sender, EventArgs e)
        {
            ManagedWebSessionContext.Bind(HttpContext.Current, SessionManager.SessionFactory.OpenSession());
        }
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.SessionFactory);
            if (session != null)
            {
                try
                {
                    if (session.Transaction != null && session.Transaction.IsActive)
                    {
                        session.Transaction.Rollback();
                    }
                    else
                    {
                        session.Flush();
                    }
                }
              finally
                {
                  session.Close();
                }
            }
        }



--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Oskar Berggren

unread,
Apr 7, 2010, 6:16:28 AM4/7/10
to nhu...@googlegroups.com
Theory: Maybe the problem is that your configuration and session factory is initialized on the first request after application restart. If a second request comes in before the first request is finished, maybe it will also try to initialize and then triggering this problem somehow.

/Oskar


2010/4/7 Daoming Yang <daomi...@googlemail.com>

Daoming Yang

unread,
Apr 7, 2010, 8:19:19 AM4/7/10
to nhu...@googlegroups.com
Thank you Tuna and Oskar, 

Now I'm planning to using NHibernate.Burrow and see whether this could resolve my Collection was modified; enumeration operation may not execute issue. 

Here is another issue "Could not load file or assembly 'NHibernate, Version=2.0.1.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4'"

How can I make the NHibernate.Burrow to work with NHibernate version 2.1.2.4000 ?

NOTE: 
  1. I have downloaded the source of the Burrow and tried to reference the Nhibernate DLL version 2.1.2.4000, but project cannot compile. 

Can anyone advise me?

Many thanks.

Daoming


Jason Dentler

unread,
Apr 7, 2010, 8:35:09 AM4/7/10
to nhu...@googlegroups.com
Daoming,

Try to use http://hornget.net to resolve those types of dependency issues.

Thanks,
Jason

Daoming Yang

unread,
Apr 7, 2010, 8:54:10 AM4/7/10
to nhu...@googlegroups.com
Thanks Jason.

I have tried. Burrow did not ship with NHibernate.

Is anyone using NHibernate 2.1.2.4000 with Burrow? Could you advise me how you configured the settings?


NOTE: 

I have tried the following setting in the Web.Config file.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4"/>
      <bindingRedirect oldVersion="2.0.1.4000" newVersion="2.1.2.4000"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>
   
Many Thanks.

Daoming

Daoming Yang

unread,
Apr 8, 2010, 4:09:05 AM4/8/10
to nhu...@googlegroups.com
HI Oskar, Tuna and Jason

Thank you very much. My problem has been resolved. The session factory and configuration were initialised for many times. Thank you all. 

Daoming
Reply all
Reply to author
Forward
0 new messages