Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InSingletonScope bug (?) when requesting an instance in OnActivation callback #221

Closed
Anlo2846 opened this issue Sep 23, 2016 · 1 comment
Assignees
Labels
Milestone

Comments

@Anlo2846
Copy link

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.

@scott-xu scott-xu self-assigned this Sep 16, 2017
@scott-xu scott-xu added the Bug label Sep 16, 2017
@scott-xu scott-xu added this to the 4.0 milestone Sep 16, 2017
@scott-xu
Copy link
Member

Fixed by 8de2b02

@scott-xu scott-xu modified the milestones: 4.0, 3.3.0 Sep 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants