Cannot pass parameters to controller constructor - Autofac MVC 5

1.2k views Asked by At

I'm trying from 16 hours to fix one issue in my application. I've searched internet but couldn't find any solution. I'm using Onion Architecture with ASP.NET MVC. I cannot inject service into my controller.

Controller:

public class TestController : Controller
{
    private readonly IService<Customer> _service;
    public TestController(IService<Customer> service)
    {
        _service = service;
    }

    public async Task<ActionResult> Index()
    {
        var list = await _service.GetAllAsync();
        return View(list);
    }
}

IOC:

public class IocConfig
    {
        public static void RegisterDependencies()
        {
            var builder = new ContainerBuilder();
            const string nameOrConnectionString = "name=AppContext";
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterGeneric(typeof(EntityRepository<>)).As(typeof(IRepository<>)).InstancePerRequest();
            builder.RegisterGeneric(typeof(Service<>)).As(typeof(IService<>)).InstancePerRequest();
            builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerRequest();
            builder.Register<IEntitiesContext>(b =>
            {
                var logger = b.Resolve<ILogger>();
                var context = new AspnetIdentityWithOnionContext(nameOrConnectionString, logger);
                return context;
            }).InstancePerRequest();
            builder.Register(b => NLogLogger.Instance).SingleInstance();
            builder.RegisterModule(new IdentityModule());
                var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }

When i run action method it gives

"No parameterless constructor defined for this object."

I'm using latest version of Autofac. Please guide me with this.

0

There are 0 answers