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
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
WhenGeting a List of Instances which contains that class
ThenNinject.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>();
The text was updated successfully, but these errors were encountered:
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
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
Possible issue when using WhenInjectedExactlyInto and Get<List>:
WhenInjectedExactlyInto
to BindGet
ing a List of Instances which contains that classNinject.ActivationException
(Several constructors have the same priority) occursExample code:
Error message:
The work around is to use
GetAll<T>
:var testClasses = kernel.GetAll<ITestClass>();
The text was updated successfully, but these errors were encountered: