Ninject 投掷“集合已修改;枚举操作可能无法执行”错误

发布于 2025-01-04 13:56:04 字数 2527 浏览 2 评论 0原文

我在我的 Web 项目中使用以下 2 个 Ninject 程序集。

Ninject.dll – Version 2.2.0.0
Ninject.Web.dll – Version 2.2.0.4

我注意到,当我在服务器上执行负载测试时,我收到指向 Ninject 的异常

消息:集合已修改;枚举操作可能不 执行。堆栈跟踪:位于 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 资源)位于 System.Collections.Generic.List1.Enumerator.MoveNextRare() 位于 System.Collections.Generic.List1.Enumerator.MoveNext() 位于 System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at Ninject.Infrastruct.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable1 系列,Action`1 动作)中 c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastruct\Language\ExtensionsForIEnumerableOfT.cs:line 22 于 Ninject.OnePerRequestModule.DeactivateInstancesForCurrentHttpRequest() 在 c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\OnePerRequestModule.cs:line 63 at Ninject.OnePerRequestModule.b__0(对象o,EventArgs e) 在 c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\OnePerRequestModule.cs:line 36 于 System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, 布尔&同步完成)

我确实在 2011 年 5 月 11 日的 Remo Gloor 中找到了这个链接,其中说有一个新的 2.4 版本,其中提到该问题将在 2.4 版本中得到修复。我不确定这个版本是否已发布/可用,因为我无法找到它。 Ninject 运行时异常频繁发生 - System.InvalidOperationException:集合已修改;枚举操作可能无法执行

有谁知道解决此 Ninject 错误的解决方案或知道在哪里可以找到 2.4 版本?


这是我使用 Ninject 2.2.0.0 版本使用 3.0 完成映射的方式,

Bind<IMyEntities>().To<MyEntities>().InRequestScope().WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString);  

我将其更改为使用

Bind<IMyEntities>().ToConstructor(x => new MyEntities()); 

or

Bind<IMyEntities>().ToConstructor(x => new MyEntities(ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString)); 

or

Bind<IMyEntities>().ToConstructor(x => new MyEntities(new EntityConnection (ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString)));  

在尝试了上述所有 3 种不同的方法后,我仍然收到错误。每次以某种方式调用第三个构造函数时。
我不知道还能尝试什么。我投入了大量时间尝试不同的事情,但仍然没有得出结论/解决方案。 如果我无法以某种方式找到/解决这个问题,我将不得不删除所有 Ninjet 引用并转而寻找其他一些 DI。

I am using the following 2 Ninject assemblies in my web project.

Ninject.dll – Version 2.2.0.0
Ninject.Web.dll – Version 2.2.0.4

I have noticed that when I perform load testing on the server I am getting exceptions in my which are pointing to the Ninject

Message: Collection was modified; enumeration operation may not
execute. Stack Trace: at
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource
resource) at
System.Collections.Generic.List1.Enumerator.MoveNextRare() at
System.Collections.Generic.List
1.Enumerator.MoveNext() at
System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at
Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable
1
series, Action`1 action) in
c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:line
22 at
Ninject.OnePerRequestModule.DeactivateInstancesForCurrentHttpRequest()
in
c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\OnePerRequestModule.cs:line
63 at Ninject.OnePerRequestModule.b__0(Object o, EventArgs
e) in
c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\OnePerRequestModule.cs:line
36 at
System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously)

I did find this link from Remo Gloor dated May 11 2011 which says there is a new 2.4 version which has mentioned that the problem will be fixed 2.4 version. I am not sure if this version is released/available yet as I not able to find it.
Ninject Runtime Exception occuring frequently - System.InvalidOperationException: Collection was modified; enumeration operation may not execute

Does anybody know the solution to this resolve this Ninject error or know where can I find the 2.4 version?


This is the way I have done the mapping using Version 2.2.0.0 of Ninject

Bind<IMyEntities>().To<MyEntities>().InRequestScope().WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString);  

Using 3.0 I changed it to use

Bind<IMyEntities>().ToConstructor(x => new MyEntities()); 

or

Bind<IMyEntities>().ToConstructor(x => new MyEntities(ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString)); 

or

Bind<IMyEntities>().ToConstructor(x => new MyEntities(new EntityConnection (ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString)));  

After trying all the above mentioned 3 different ways I am still getting the error. Every time the 3rd constructor is getting called somehow.
I am not sure what else to try. I have kind of invested a very good amount of time trying different things and still not coming to a conclusion/resolution.
If I am not able to find/resolve this problem somehow I will have to remove all the Ninjet references all together and go for some other DI's.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2025-01-11 13:56:04

由于一些小的接口不兼容,2.4 已变成 3.0。请参阅http://www.nuget.org/packages/Ninject/3.0.0- rc3

2.4 has become 3.0 because of some minor interface incompatibilities. See http://www.nuget.org/packages/Ninject/3.0.0-rc3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文