0

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));
        }

    }
}
mgh75
  • 77
  • 3
  • 11
  • you need to register `ICustomerService ` in autofac something like `builder.RegisterType().As();` please go through [this](http://autofaccn.readthedocs.io/en/latest/register/registration.html) documentation – Curiousdev Nov 29 '17 at 10:36
  • 1
    Thanks, thought I'm led to believe that my code above automatically registers assemblies? – mgh75 Nov 29 '17 at 10:37
  • `builder.RegisterControllers(typeof(MvcApplication).Assembly);` Is `CustomerController` in the same assembly as `MvcApplication`? Please show us how the source code for `ICustomerService` and its implementation. – mjwills Nov 29 '17 at 10:38
  • This is the code for ICustomerService `using NorthwindTest.Core.Interfaces; using NorthwindTest.Repository.DataModel; using System; using System.Collections.Generic; using System.Text; namespace NorthwindTest.Services.Customers { public interface ICustomerService : ITransientDependency { IEnumerable GetCustomers(); } }` – mgh75 Nov 29 '17 at 10:44
  • @mjwills 's concern is also valid recheck is your controller is in same assembly which you are registering – Curiousdev Nov 29 '17 at 10:46
  • OK now sorted, it was as simple as my DependencyConfig.cs not being declared in Global.asax.cs Thanks for your help – mgh75 Nov 29 '17 at 11:15
  • For these kinds of issues, breakpoints and debugging can come in handy - to verify that your code is actually being executed. – mjwills Nov 29 '17 at 11:18

0 Answers0