.NET memory profiling / risks for leaks / Ninject / Direct delegate roots

感情迁移 提交于 2019-12-11 08:56:56

问题


I am profiling an application that uses Ninject for DI. Over the course of time, I am seeing lots of instances of the BindingBuilder class, which is used to define the objects defined in the container.

Ninject's "ToMethod" binding is used to define all the objects in the container. The context available in the lamba to access the kernel is then used to retrieve other objects from the container. Example:

Bind<IService>()
    .ToMethod(ctx => new CustomService(
        ctx.Kernel.GetDefault<IOtherService>(), 
        ctx.Kernel.GetDefault<IAnotherService>()
    ))
    .InSingletonScope();

Is it normal behaviour to see BindingBuilders increasing over time or should these references exist only once?


回答1:


ToMethod creates a new anonymous class within the BindingBuilder for the lambda expression. What you see is not an instance of BindingBuilder but something like BindingBuilder<T>+c__DisplayClass1<IService>

Furthermore you aren't using Ninject as intended. The binding above can be written much easier. Let Ninject decide what to inject instead of adding an explizit definition.

Bind<IService>().To<CustomService>.InSingletonScope();


来源:https://stackoverflow.com/questions/6607274/net-memory-profiling-risks-for-leaks-ninject-direct-delegate-roots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!