Tag Archives: Unity

Unity – The type Interface does not have an accessible constructor

Had a constructor accepting string parameter

public CustomerRepo(string dbContext)
{
	if (dbContext == "AWContext")
	{
		_context = new AWContext();
	}
	else
	{
		throw new ArgumentException("Invalid dbContext name");
	}
}

registered following instructions from MSDN

container.RegisterType<ICustomerRepo, CustomerRepo>(
    "test", 
    new InjectionConstructor("AWContext")
);

The “test” is just a name of current mapping. But it didn’t work until I completely removed it(!):

container.RegisterType<ICustomerRepo, CustomerRepo>(
    new InjectionConstructor("AWContext")
);