A slightly contrived example exhibiting a strange behaviour of InSingletonScope() in Ninject v3.2.2. Our actual code involves several more classes but we get the same problem, a singleton scope class in created twice.
[Test]public void NinjectShouldCreateOnlyOneHandlerInstance(){using(IKernel kernel = new StandardKernel()) {
kernel.Bind<IHandler>().ToSelf().InSingletonScope();kernel.Bind<Module>().ToSelf().InSingletonScope().OnActivation(_ => kernel.Get<IHandler>());Assert.True(kernel.Get<IHandler>()!=null);
}
public class Handler: IHandler
{public Handler(Module module){Debug.WriteLine("Handler ctor");}}public class Module{public Module(){Debug.WriteLine("Module ctor");}}
Unfortunately I can not provide any solution.
--
You received this message because you are subscribed to the Google Groups "ninject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ninject+unsubscribe@googlegroups.com.
To post to this group, send email to nin...@googlegroups.com.
Visit this group at https://groups.google.com/group/ninject.
For more options, visit https://groups.google.com/d/optout.
Hi Assil,I refactored the code and placed all the bindings in modules. I have two, one for the main program and another for a plugin. Same behavior. I removed a setter property, figuring maybe that was confusing NInject, since some IoC containers try to inject properties. Same behavior. Are there any debugging switches or techniques for logging the chains of activation of objects and perhaps determine what is triggering the constructor to be called 3 times? Thank you.
On Mon, Apr 2, 2018 at 2:12 PM, Assil Abdulrahim <abdulra...@gmail.com> wrote:
Do not use Kernel.Create your own class like so:public class Bindings : NinjectModule{Bind<IObjectManager>().To<ObjectManager>().InSingletonScope();}On Monday, April 2, 2018 at 5:00:24 PM UTC-4, Frederick Brier wrote:I may be seeing the same issue in Ninject 3.3.4, but am not even using the OnActivation(). It is just constructor injection, although I do use the .NET Core GetRequiredService() extension which is using a thin IServiceProvider wrapper around IKernel to fetch the object in the IApplicationLifecycle methods.
_kernel.Bind<IObjectManager>().To<ObjectManager>().InSingletonScope();The ObjectManager constructor is actually called 3 times. Has anyone have a workaround or a fix? Thank you.
--
You received this message because you are subscribed to the Google Groups "ninject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.