.NET memory profiling / risks for leaks / Ninject / Direct delegate roots
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 refer开发者_如何学运维ences exist only once?
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();
精彩评论