Is this an InSingletonScope() bug?

427 views
Skip to first unread message

Anlo

unread,
Sep 22, 2016, 8:31:01 AM9/22/16
to ninject
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 the "original" request (called from the test method) should return the instance created by the OnActivation callback.

Valery Shchepashchenko

unread,
Dec 8, 2016, 12:56:08 AM12/8/16
to ninject


On Thursday, September 22, 2016 at 3:31:01 PM UTC+3, Anlo wrote:
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");
            }
        }
Message has been deleted

TWischmeier

unread,
May 16, 2017, 11:58:09 AM5/16/17
to ninject
Unfortunately I can not provide any solution. 

We also observe the same Problem using Ninject 3.2.2. 

Frederick Brier

unread,
Apr 2, 2018, 5:00:24 PM4/2/18
to ninject
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.
Message has been deleted

Assil Abdulrahim

unread,
Apr 2, 2018, 5:12:28 PM4/2/18
to ninject

Do not use Kernel.

Create your own class like so:

 public class Bindings : NinjectModule
{
         Bind<IObjectManager>().To<ObjectManager>().InSingletonScope(); 

Frederick Brier

unread,
Apr 2, 2018, 6:54:15 PM4/2/18
to nin...@googlegroups.com
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.

--
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.

Assil Abdulrahim

unread,
Apr 2, 2018, 7:49:46 PM4/2/18
to ninject
Hi Fred
Are you able to upgrade to version="4.0.0-beta-0134" ????
It may be a bug in the older version that I am not aware of.


On Monday, April 2, 2018 at 6:54:15 PM UTC-4, Frederick Brier wrote:
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.

Frederick N. Brier

unread,
Apr 2, 2018, 9:34:03 PM4/2/18
to nin...@googlegroups.com
I am hesitant to use a beta version. How stable is the 4.0.0 beta? If you feel it is pretty solid, I am more than willing to try and see if it fixes the problem. Thank you for the recommendation.

Frederick Brier

unread,
Apr 6, 2018, 3:17:25 PM4/6/18
to ninject
This appears to be a defect in Ninject. A couple of posts [http://kerryritter.com/ninject-insingletonscope-still-creates-new-objects/, https://stackoverflow.com/questions/12784447/singleton-scope-binding-not-working-as-intended] complaining of singletons creating multiple instances when using an interface for the DI binding. This clued me into the idea the problem might be related to the search of the scope being unsuccessful when specifying an interface. I still wanted to have Ninject create the instance, so I tried the new syntax:

_kernel.Bind<IObjectManager,ObjectManager>().To<ObjectManager>().InSingletonScope();

This appears to be a successful workaround. If I have time, I will attempt to figure out the source of the problem. Thank you for your suggestions.
Reply all
Reply to author
Forward
0 new messages