Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Autofac with ASP.NET MVC 5

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??

like image 999
TanvirArjel Avatar asked Nov 04 '16 12:11

TanvirArjel


People also ask

Should I use Autofac with ASP.NET core?

Autofac is the most widely used DI/IoC container for ASP.NET, and it is fully compatible with.NET Core as well. . NET Core has a built-in dependency injection framework that is ready to use. Even though the default DI may provide sufficient functionality, there are several limitations when using it.


1 Answers

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

like image 147
Ian Mercer Avatar answered Nov 10 '22 04:11

Ian Mercer