You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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()
{
IKernel kernel = new StandardKernel();
kernel.Bind<Handler>().ToSelf().InSingletonScope();
kernel.Bind<Module>().ToSelf().InSingletonScope().OnActivation(_ => kernel.Get<Handler>());
kernel.Get<Handler>();
}
public class Handler
{
public Handler(Module module)
{
Debug.WriteLine("Handler ctor");
}
}
public class Module
{
public Module()
{
Debug.WriteLine("Module ctor");
}
}
Debug output:
Module ctor
Handler ctor
Handler ctor
I think the problem is that a Handler is requested while it is being created. But I would expect the original request (called from the test method) to return the instance created by the OnActivation callback. Or if it isn't possible, to throw an exception rather than creating the singleton scope class twice.
The text was updated successfully, but these errors were encountered:
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.
Debug output:
I think the problem is that a Handler is requested while it is being created. But I would expect the original request (called from the test method) to return the instance created by the OnActivation callback. Or if it isn't possible, to throw an exception rather than creating the singleton scope class twice.
The text was updated successfully, but these errors were encountered: