i am trying to imitate the sample given with using autofac, but constantly getting no parameterless constructor missing error for my contoller. what am i doing wrong should i create a modelbinder for the irepository?
here is how the bootstrapper looks like. mocked testers put in here it s just as in example does not really reflect the actual intention. there gonna a seperate test project.
Mock<iproductrepository> mock = new Mock<iproductrepository>();
mock.Setup(m => m.products).Returns(new List<product>()
{
new product() {name = "football", price = 25},
new product() {name = "surf board",price = 179},
new product() {name = "running shoes", price = 95}
}.AsQueryable()
);
builder.RegisterInstance(mock.Object).As<iproductrepository>();
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
and here is controller simple
public class productcontroller : Controller
{
private readonly iproductrepository _productrepository;
/// <summary>
///
/// </summary>
/// <param name="productrepository"></param>
public productcontroller(iproductrepository productrepository)
{
_productrepository = productrepository;
}
//
// GET: /product.controller/
public ViewResult list()
{
return View(_productrepository.products);
}
}
getting this error
No parameterless constructor defined for this object. Description: An
unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code. Exception Details:
System.MissingMethodException: No parameterless constructor defined
for this object. Source Error: An unhandled exception was generated
during the execution of the current web request. Information regarding
the origin and location of the exception can be identified using the
exception stack trace below. Stack Trace: [MissingMethodException: No
parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean
publicOnly, Boolean noCheck, Boolean& canBeCached,
RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean
skipCheckThis, Boolean fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,
Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean
fillCache) +241 System.Activator.CreateInstance(Type type, Boolean
nonPublic) +69
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext
requestContext, Type controllerType) +67 [InvalidOperationException:
An error occurred when trying to create a controller of type
‘*.productcontroller’. Make sure that the controller has
Autofac in the method
builder.RegisterControllersregisters the controllers which name end with “Controller” with a capital C :So you need to rename your controller to
productController(anyway in C#ProductControlleraccording to the naming convenctions) or register the your controllers by “hand” in the container.