e36*_*6M3 7 .net c# asp.net ninject ninject-2
我有一段代码Parallel.Foreach在要处理的项目列表上运行.每次迭代都会创建一些对象,每个对象都会实例化并处理它自己的Ninject IKernel实例.当对象完成它的工作时,就会释放IKernel.
也就是说,这个代码在我的Windows 7,I7笔记本电脑上运行得非常好.但是,当我将其推送到运行Windows 2008的VPS时,我得到了这个例外.异常不会发生在同一个迭代中,有时会经历10次迭代并抛出异常,有时则会经历数百次异常.显然似乎是一个线程问题,但除了我的VPS之外,它不会发生.如果重要,那么它将在ASP.NET IIS中托管.
System.AggregateException: One or more errors occurred. --->
System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.Generic.List`1.RemoveAt(Int32 index)
at Ninject.KernelBase.Dispose(Boolean disposing)
Run Code Online (Sandbox Code Playgroud)
以下是代码片段:
//Code that creates and disposes the Ninject kernel
using(ninjectInstance = new NinjectInstance())
{
using (var unitOfWork = ninjectInstance.Kernel.Get<NinjectUnitOfWork>())
{
Init();
continueValidation = Validate(tran, ofr);
}
}
public class NinjectInstance : IDisposable
{
public IKernel Kernel { get; private set; }
public NinjectInstance()
{
Kernel = new StandardKernel(
new NinjectSettings() { AllowNullInjection = true },
new NinjectUnitOfWorkConfigModule());
}
public void Dispose()
{
if (Kernel != null)
{
Kernel.Dispose();
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑1 有一件事是肯定的,这是一个线程安全问题,我不应该为每个应用程序创建多个IKernel实例.理解如何配置适当的范围以完成实体框架上下文线程安全性,同时保留UoW类型方法,其中多个业务层类可以在单个线程内的UoW范围内共享相同的EF上下文.
请参阅http://groups.google.com/group/ninject/browse_thread/thread/574cd317d609e764
正如我告诉你的,Ninject的ctor不是线程安全的atm,除非你使用NOWEB!如果多次创建/处置内核,则必须自己同步访问权限!我仍然建议你重新设计你的UoW实现!