I have a controller that has a dependency.
However, when navigating to the page I get "No parameterless constructor defined for this object." error.
Does anyone have any suggestions as to why this may be? I've posted the code for the CustomerController and DependencyConfig below.
Many thanks.
CustomerController.cs
namespace NorthwindTest.Web.Controllers
{
public class CustomerController : Controller
{
private readonly ICustomerService service;
public CustomerController(ICustomerService service)
{
this.service = service;
}
public ActionResult Index()
{
return View();
}
public ActionResult GetCustomers([DataSourceRequest]DataSourceRequest request)
{
var customers = service.GetCustomers().ToList();
var customerViews = Mapper.Map<List<Customer>, List<CustomerViewModel>>(customers);
return Json(customerViews.ToDataSourceResult(request));
}
}
}
Autofac is installed and set up in DependencyConfig.cs
namespace NorthwindTest.Web.App_Start
{
public class DependencyConfig
{
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
builder.RegisterType<Entities>()
.As<IUnitOfWork>()
.InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(Repository<>))
.As(typeof(IRepository<>))
.InstancePerDependency();
var assembies = BuildManager.GetReferencedAssemblies().Cast<Assembly>()
.Where(a => a.GetName().Name.StartsWith("NorthwindTest."))
.ToArray();
builder.RegisterAssemblyTypes(assembies)
.Where(t => t.IsAssignableTo<ITransientDependency>())
.AsImplementedInterfaces()
.InstancePerDependency();
builder.RegisterAssemblyTypes(assembies)
.Where(t => t.IsAssignableTo<ISingletonDependency>())
.AsImplementedInterfaces()
.SingleInstance();
builder.RegisterAssemblyTypes(assembies)
.Where(t => t.IsAssignableTo<ILifetimeScopeDependency>())
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}