gpt4 book ai didi

NHibernate:集合已修改;枚举操作可能无法执行

转载 作者:行者123 更新时间:2023-12-03 22:23:59 24 4
gpt4 key购买 nike

我目前正在努力解决此“集合已被修改;枚举操作可能无法执行”的问题。

我已经搜索了此错误消息,并且都与foreach语句有关。我确实有一些foreach语句,但它们只是表示数据。我没有使用任何在foreach语句中删除添加

笔记:

  • 错误随机发生(每天大约4-5次)。
  • 该应用程序是MVC网站。
  • 大约有5个用户在运行此应用程序(每天约有150个订单)。可能是其他用户修改了该集合,然后发生此错误吗?
  • 我有log4net设置,可以找到设置here
  • 确保 Controller 具有无参数的公共(public)构造函数,我在AdminProductController中确实有无参数的公共(public)构造函数。

    有谁知道为什么会这样以及如何解决这个问题?

    一位 friend (奥斯卡)提到

    "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."



    非常感谢。

    道明

    这是错误消息:

    System.InvalidOperationException
    收藏集已修改;枚举操作可能无法执行。
    System.InvalidOperationException:尝试创建类型为'WebController.Controllers.Admin.AdminProductController'的 Controller 时发生错误。确保 Controller 具有无参数的公共(public)构造函数。 ---> 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,字符串名称)
    在NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream,字符串名称)
    ---内部异常堆栈跟踪的结尾---
    在NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
    在NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream,字符串名称)
    在NHibernate.Cfg.Configuration.AddResource(字符串路径,程序集程序集)
    在NHibernate.Cfg.Configuration.AddAssembly(组装程序集)
    在DomainModel.RepositoryBase..ctor()
    在WebController.Controllers._baseController..ctor()
    在WebController.Controllers.Admin.AdminProductController..ctor()
    在System.RuntimeType.CreateInstanceImpl处( bool publicOnly, bool skipVisibilityChecks, bool fillCache)
    ---内部异常堆栈跟踪的结尾---
    在System.RuntimeType.CreateInstanceImpl处( bool publicOnly, bool skipVisibilityChecks, bool fillCache)
    在System.Activator.CreateInstance(Type type,Boolean nonPublic)
    在System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext,类型controllerType)
    ---内部异常堆栈跟踪的结尾---
    在System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext,类型controllerType)
    在System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,字符串controllerName)
    在System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController& Controller ,IControllerFactory&工厂)
    在System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)
    在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤, bool 值和已完成同步)

  • 最佳答案

    奥斯卡(Oskar)是对的。两个单独的线程试图同时初始化 session 工厂。建议您对初始化代码进行一些锁定,也许仅使用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;
    }
    }
    }
    }

    关于NHibernate:集合已修改;枚举操作可能无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2593453/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com