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

WhenInjectedExactlyInto: Ninject.ActivationException (Several constructors have the same priority) occurs, when using Get<List<T>> #237

Closed
rj-xy opened this issue May 5, 2017 · 1 comment
Assignees
Labels
Milestone

Comments

@rj-xy
Copy link

rj-xy commented May 5, 2017

Possible issue when using WhenInjectedExactlyInto and Get<List>:

  • Given a dependency for a specific class using WhenInjectedExactlyInto to Bind
  • Given a class with a dependency on the previous Given
  • When Geting a List of Instances which contains that class
  • Then Ninject.ActivationException (Several constructors have the same priority) occurs

Example code:

    class Program
    {
        static void Main(string[] args)
        {
            var kernel = new StandardKernel();

            kernel.Bind<ITestClass>().To<TestClass1>();
            kernel.Bind<ITestClass>().To<TestClass2>();
            kernel.Bind<int>().ToConstant(1).WhenInjectedExactlyInto<TestClass1>();
            kernel.Bind<string>().ToConstant("2").WhenInjectedExactlyInto<TestClass2>();

            var testClasses = kernel.Get<List<ITestClass>>();
            
            // Work around:
            // var testClasses = kernel.GetAll<ITestClass>();
        }
    }

    interface ITestClass
    {
        int Number { get; }
    }

    class TestClass1 : ITestClass
    {
        public int Number { get; }

        public TestClass1(int number)
        {
            Number = number;
        }
    }

    class TestClass2 : ITestClass
    {
        public int Number { get; }

        public TestClass2(string number)
        {
            Number = Convert.ToInt32(number);
        }
    }

Error message:

Unhandled Exception: Ninject.ActivationException: Error activating List{ITestClass} using implicit self-binding of List{ITestClass}
Several constructors have the same priority. Please specify the constructor using ToConstructor syntax or add an Inject attribute.

Constructors:
[__DynamicallyInvokable]List`1(int capacity)
[__DynamicallyInvokable]List`1(IEnumerable{ITestClass} collection)

Activation path:
  1) Request for List{ITestClass}

Suggestions:
  1) Ensure that the implementation type has a public constructor.
  2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.

   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__94`1.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)

The work around is to use GetAll<T>: var testClasses = kernel.GetAll<ITestClass>();

@rj-xy rj-xy changed the title WhenInjectedExactlyInto WhenInjectedExactlyInto: Ninject.ActivationException (Several constructors have the same priority) occurs, when multiple constructors with same number of paramaters May 5, 2017
@rj-xy rj-xy changed the title WhenInjectedExactlyInto: Ninject.ActivationException (Several constructors have the same priority) occurs, when multiple constructors with same number of paramaters WhenInjectedExactlyInto: Ninject.ActivationException (Several constructors have the same priority) occurs, when using Get<List<T>> May 5, 2017
@BrunoJuchli
Copy link
Contributor

In the meantime, you've edited the code:

kernel.Bind<int>().ToConstant(1).WhenInjectedExactlyInto<TestClass1>();
kernel.Bind<string>().ToConstant("2").WhenInjectedExactlyInto<TestClass2>();

Before, both weere WhenInjectedExactlyInto<TestClass1>. Is the exception still happening on using Get<List<T>>()?

What happens when removing the bindings for TestClass2 and doing Get<ITestClass>()?

@scott-xu scott-xu self-assigned this Sep 17, 2017
@scott-xu scott-xu added the Bug label Sep 17, 2017
@scott-xu scott-xu added this to the 4.0 milestone Sep 17, 2017
@scott-xu scott-xu modified the milestones: 4.0, 3.3.0 Sep 23, 2017
glenkeane-94 pushed a commit to glenkeane-94/Nin-ject that referenced this issue Jun 17, 2022
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

3 participants