7

I am trying to implement Dependency Injection with Autofac in an ASP.NET MVC5 Project. But I am getting the following error every time:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyProjectName.DAL.Repository` ........

My Autofac configuration code in App_Start folder as follows:

public static class IocConfigurator
    {
        public static void ConfigureDependencyInjection()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterType<Repository<Student>>().As<IRepository<Student>>();

            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }      
    }

In Global.asax file:

public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            // Other MVC setup

            IocConfigurator.ConfigureDependencyInjection();
        }
    }

Here is my IRepository:

public interface IRepository<TEntity> where TEntity: class 
    { 
        IQueryable<TEntity> GelAllEntities();
        TEntity GetById(object id);
        void InsertEntity(TEntity entity);
        void UpdateEntity(TEntity entity);
        void DeleteEntity(object id);
        void Save();
        void Dispose();
    }

Here is my Repository:

public class Repository<TEntity> : IRepository<TEntity>, IDisposable where TEntity : class
    {
        internal SchoolContext context;
        internal DbSet<TEntity> dbSet;

        public Repository(SchoolContext dbContext)
        {
            context = dbContext;
            dbSet = context.Set<TEntity>();
        }
.....................
}

Here is my Student Controller:

public class StudentController : Controller
    {

        private readonly IRepository<Student> _studentRepository;
        public StudentController()
        {

        }
        public StudentController(IRepository<Student> studentRepository)
        {
            this._studentRepository = studentRepository;
        }
       ....................
}

What's wrong in my Autofac Configuration..Any Help Please??

4
  • What does the constructor of your controller class look like? Does it depend on the interface type IRepository or the concrete type Repository? And what does the constructor on the repository class look like? Please post a complete example. Commented Nov 4, 2016 at 12:13
  • @IanMercer Question is Edited..Please see now.. Commented Nov 4, 2016 at 12:17
  • Where do you register the SchoolContext with Autofac? Without that (presumably as a PerHttpRequest registration) it cannot create the Repository. Commented Nov 4, 2016 at 12:46
  • No where!! Tell me what I have to do according to my code!! Commented Nov 4, 2016 at 12:51

1 Answer 1

11

To inject a dependency you need to have satisfied all of the dependencies for all of the pieces down the chain.

In your case, the Repository constructor cannot be satisfied without a SchoolContext.

So in your registration add:

  builder.RegisterType<SchoolContext>().InstancePerRequest();

See http://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-per-request

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! It works!! Although I found the solution in a code project article just few seconds before seeing your answer!! :) Cheers!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.