ray abu
ray abu

Reputation: 143

WebApi Controller error instantiating List`1 has multiple constructors of length 1

My WebApi controller(MenusController) is failing to instantiate with the error message "instantiating List`1 has multiple constructors of length 1". the dependency graph: I'm Using Unity for dependecy injection.

MenusController > IMenuService > IMenuRepository > IEntitiesDBContext

All Classes here, take 1 dependency apart from MenuRepository that takes in two.

public class MenuRepository : IMenuRepository
{
    IMonopolyEntitiesDbContext _dbContext;
    List<MenuLink> _allsubMenus;
    public MenuRepository(IMonopolyEntitiesDbContext context, List<MenuLink> allsubMenus)
    {
        _dbContext = context;
        _allsubMenus = allsubMenus;
    }

   //Additional Code.
}


public partial class MonopolyEntities : DbContext, IMonopolyEntitiesDbContext 
{
    public MonopolyEntities()
        : base("name=MonopolyEntities")
    {
    }
}

Unity Registration

public static class WebApiHelper
{
    public static void  RegisterWebApiTypes(IUnityContainer container)
    {
        container.RegisterType<IMenuServices, MenuServices>(new   PerThreadLifetimeManager());
        container.RegisterType<MenuLink>();
        container.RegisterType<IList<MenuLink>>(new InjectionFactory( x => new List<MenuLink>()));
        container.RegisterType<IMonopolyEntitiesDbContext, MonopolyEntities>(new PerThreadLifetimeManager());
        container.RegisterType<IMenuRepository, MenuRepository>(new PerThreadLifetimeManager(), new InjectionConstructor(typeof(IMonopolyEntitiesDbContext), typeof(List<MenuLink>)));

    }
}

Here's the stack trace

Resolution of the dependency failed, type = "Monopoly.WebApi.MenusController", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type List1 has multiple constructors of length 1. Unable to disambiguate. ----------------------------------------------- At the time of the exception, the container was: Resolving Monopoly.WebApi.MenusController,(none) Resolving parameter "menuServices" of constructor Monopoly.WebApi.MenusController(Monopoly.BLL.Interfaces.IMenuServices menuServices) Resolving Monopoly.BLL.MenuServices,(none) (mapped from Monopoly.BLL.Interfaces.IMenuServices, (none)) Resolving parameter "repository" of constructor Monopoly.BLL.MenuServices(Monopoly.DAL.Interfaces.IMenuRepository repository) Resolving Monopoly.DAL.MenuRepository,(none) (mapped from Monopoly.DAL.Interfaces.IMenuRepository, (none)) Resolving parameter "allsubMenus" of constructor Monopoly.DAL.MenuRepository(Monopoly.DAL.Interfaces.IMonopolyEntitiesDbContext context, System.Collections.Generic.List1[[Monopoly.DAL.MenuLink, Monopoly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] allsubMenus) Resolving System.Collections.Generic.List`1[Monopoly.DAL.MenuLink],(none)

I know Having two constructors with parameter list of equal length creates an ambiguity, and in that case I need to explicitly select the constructor to use. But I don't have a type here that has more than 1 constructor.

Any Ideas??

Upvotes: 0

Views: 705

Answers (1)

Rhumborl
Rhumborl

Reputation: 16609

Your class is expecting a parameter of type class List<MenuLink>, but your Unity container only has a registration for the interface IList<MenuLink>.

Having a List<> as a constructor parameter doesn't smell quite right to me anyway but if that is how you want it, I would suggest changing your repository constructor to take the interface instead of the class. The Unity should be able to construct it using the InjectionFactory to provided:

public MenuRepository(IMonopolyEntitiesDbContext context, IList<MenuLink> allsubMenus)
{
    _dbContext = context;
    _allsubMenus = allsubMenus;
}

and (I think)

container.RegisterType<IMenuRepository, MenuRepository>(new PerThreadLifetimeManager(), new InjectionConstructor(typeof(IMonopolyEntitiesDbContext), typeof(IList<MenuLink>)));

Upvotes: 2

Related Questions