Ninject垃圾收集

我在一个由服务,存储库组成的n层应用程序中使用Ninject,所有这些都与UnitOfWork模式和Ninject相连。 此外,我在引用这些服务和存储库的单独线程中执行不同的作业。

每隔一段时间,似乎在随机的时间,我得到一个exception,它崩溃我的控制台应用程序执行作业。 例外是:

Application: Playground.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException Stack: at Ninject.Activation.Caching.GarbageCollectionCachePruner.PruneCacheIfGarbageCollectorHasRun(System.Object) at System.Threading.ExecutionContext.runTryCode(System.Object) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading._TimerCallback.PerformTimerCallback(System.Object) 

据我所知,这与Ninject中新的Cache-and-Collection管理有关。 但是,我没有为任何Ninject绑定指定任何范围。

编辑:代码示例:

以下是一些使用的代码(我相信关键部分):

  public class DefaultUnitOfWork : Provider, IUnitOfWork, IServiceUnitOfWork where TObjectContext : ObjectContext, new() { public DefaultUnitOfWork(){ _kernel = new CustomKernel(new CommonRepositoryModule(), new ServiceModule(), (NinjectModule)kernel.Get()); } } 

我确实在一段时间之前检查了源代码,找到了我的另一个问题。 基本上,以前有多个kernel被创建并且没有这样处理,以便过度写入这些行为并处理它们我实现了IDisposable并且我在内核中调用了Dispose(bool disposing)方法。

  public void Dispose() { if (_kernel != null) _kernel.Dispose(true); } 

我希望那些样品很方便。

编辑2

我发现了这个问题,这完全是我的错。 由于我有一些范围和处理问题,我修改了代码,所以我按照上面提到的方式处理Dispose(true)。 这搞乱了Ninject的缓存机制。 我重新实现了大部分代码并没有问题。 但是感谢你的想法。