【发布时间】:2015-11-12 16:15:25
【问题描述】:
这个问题更像是一个“我该怎么做?”,而不是一个“我做错了什么?”。我有一个名为 QueryProcessor 的类来处理查询(想想 CQRS)。该对象被注入到我的演示者中。 QueryProcessor 需要使用内核来解析绑定。直接或通过工厂注入内核很容易。这样做不会导致内存泄漏是诀窍。
我已经使用内存分析器验证了我的 QueryProcessor 对象都没有被垃圾回收。该类如下所示:
public sealed class QueryProcessor : IQueryProcessor, IDisposable
{
private readonly IKernelFactory _container;
private bool _disposed;
public QueryProcessor(IKernelFactory container)
{
_container = container;
}
//[DebuggerStepThrough]
public TResult Process<TResult>(IQuery<TResult> query)
{
var handlerType = typeof(IQueryHandler<,>).MakeGenericType(query.GetType(), typeof(TResult));
dynamic handler = _container.RetrieveKernel().Get(handlerType);
return handler.Handle((dynamic)query);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing && !_disposed)
{
// dispose of stuff here
_disposed = true;
}
}
}
public interface IKernelFactory
{
IKernel RetrieveKernel();
}
我的作文根相当简单。我正在使用 Ninject 的工厂扩展。
public void OnLoad(IKernel kernel)
{
// Auto-Register all the validators which are stored in the Service assembly.
AssemblyScanner.FindValidatorsInAssembly(_serviceAssembly).ForEach(
result => kernel.Bind(result.InterfaceType, result.ValidatorType)
);
ManualRegistrations(kernel);
kernel.Bind<IKernelFactory>().ToFactory();
AutoRegisterType(kernel, typeof(IQueryHandler<,>));
AutoRegisterType(kernel, typeof(ICommandHandler<>));
}
如前所述,注入是有效的,但它会留下内存泄漏。我应该如何让 Ninject 内核解析我的 QueryProcessor 中的内容而不导致泄漏?
谢谢
更新 - 新问题
我试图通过创建一个带有新模块的新内核来解决这个问题,该模块与合成根的主内核分开。这些子内核将被创建和处理,它们的生命周期与 QueryProcessor 的生命周期相关联。我在主模块中是这样连接的:
kernel.Bind<IQueryProcessor>().ToMethod(ctx => new QueryProcessor(new StandardKernel(new ProcessorModule(_serviceAssembly)))).InTransientScope();
在第一次处理内核之前它可以正常工作。但在那之后,我收到以下错误消息:
Error loading Ninject component ICache
No such component has been registered in the kernel's component container.
Suggestions:
1) If you have created a custom subclass for KernelBase, ensure that you have properly
implemented the AddComponents() method.
2) Ensure that you have not removed the component from the container via a call to RemoveAll().
3) Ensure you have not accidentally created more than one kernel.
如果我这样做该死,如果我不这样做该死......
【问题讨论】:
-
如何绑定QueryProcessor?谁在处理查询处理器?
-
还要注意,如果没有内存压力,垃圾收集器不收集对象是完全合法的。等到您不再引用某个对象,然后将其视为内存泄漏是无效。那么你有什么证据证明确实存在内存泄漏?由于您使用了内存分析器,请向我们展示
QueryProcessor的 GC 根的所有路径(应该收集但不收集)。 -
@BatteryBackupUnit QueryProcessor 与 kernel.bind
.To () 绑定,并由 Presenter 在各自的 Dispose 方法中处理。但是,我不能处理内核,因为其他地方需要它。注意,这不是一个 web 项目,所以内核需要比 http 请求/响应更长的寿命。今晚我将尝试从分析器中获取一些数据。但它确实有一个强制 GC 的按钮,并且该按钮对所有其他 gen 1 对象有效,这些对象在 GC 之后从下一个内存快照中消失。 -
使用此配置,ninject 不会保留对
QueryProcessor的引用。如果存在内存泄漏,则它必须影响更多类型(例如,绑定InSingletonScope()的东西会保留在QueryProcessors 上)或者内存泄漏是由于您的代码的某些部分造成的。 -
旁注:即使对于 Web 应用程序,为每个请求重新创建内核也不是一个好主意(性能方面)。虽然有些人可能仍然这样做,但肯定有很多人不这样做。
标签: c# dependency-injection ninject ninject-extensions