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
public class IoC
{
public static IContainer Init()
{
var container = new Container(x =>
{
x.Scan(s => {
s.TheCallingAssembly();
s.AssembliesFromApplicationBaseDirectory();
s.WithDefaultConventions();
});
x.For<IActivityService>().Singleton();
x.ForConcreteType<ClassWithTwoConstructors>()
.Configure.SelectConstructor(() => new ClassWithTwoConstructors(new AnotherDependency()))
.Ctor<AnotherDependency>("anotherDependency").Is(new AnotherDependency());
});
container.WhatDoIHave();
return container;
}
}
public class AnotherDependency
{
public AnotherDependency()
{
}
}
public class ClassWithTwoConstructors
{
public ClassWithTwoConstructors(int age, string name)
{
}
public ClassWithTwoConstructors(AnotherDependency anotherDependency)
{
}
}
public class MainService
{
public MainService(IStoryService storyService,
ClassWithTwoConstructors anotherDependency)
{
}
}
The MainService constructor cannot be instantiated with the AnotherDependency parameter.
The text was updated successfully, but these errors were encountered:
jonswaino
changed the title
Class with multiple param constructor cannot be selected if parameter is not a primitive type
Class with multiple constructors cannot be selected if parameter is not a primitive type
Jan 26, 2015
It was a bit of an edge case with the constructor selection. My expression visitor was finding the inner constructor in this case. Without waiting for 3.1.5, you can work around it by:
varcontainer=new Container(x =>{ x.Scan(s =>{ s.TheCallingAssembly(); s.AssembliesFromApplicationBaseDirectory(); s.WithDefaultConventions();}); x.For<IActivityService>().Singleton(); x.ForConcreteType<ClassWithTwoConstructors>().Configure.SelectConstructor(()=>new ClassWithTwoConstructors(new AnotherDependency())).Ctor<AnotherDependency>().Is(anotherDependency);// You don't need to specify the argument name if there is only 1 for that type//.Ctor<AnotherDependency>("anotherDependency").Is(anotherDependency);});
The MainService constructor cannot be instantiated with the AnotherDependency parameter.
The text was updated successfully, but these errors were encountered: