None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type

autofac c# entity-framework

Question

I have read many questions about this problem on SO but I could not figure out what is the issue with my implementation.

I am getting following exception:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'SampleAuthTemplate.Domain.Entities.Core.Repositories.Concrete.EntityRepository`1[SampleAuthTemplate.Domain.Entities.Order]' can be invoked with the available services and parameters: ↵Cannot resolve parameter 'SampleAuthTemplate.Domain.Entities.Core.EntitiesContext entitiesContext' of constructor 'Void .ctor(SampleAuthTemplate.Domain.Entities.Core.EntitiesContext)'."

Following is the AutoFac registrations:

private static IContainer RegisterServices(ContainerBuilder builder)
        {
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            // registration goes here
            builder.RegisterType<EntitiesContext>().As<IdentityDbContext<ApplicationUser>>().InstancePerApiRequest();
            builder.RegisterGeneric(typeof(EntityRepository<>)).As(typeof(IEntityRepository<>)).InstancePerApiRequest();
            //builder.RegisterGeneric(typeof(AuthRepository<>)).As(typeof(IAuthRepository<>)).InstancePerApiRequest();

            builder.RegisterType<AuthRepository>().As<IAuthRepository>().InstancePerApiRequest();

            builder.RegisterType<OrderService>().As<IOrderService>().InstancePerApiRequest();
            return builder.Build();
        }

and EntitiesContext class:

public class EntitiesContext : IdentityDbContext<ApplicationUser>
    {
        public EntitiesContext(): base("SampleApp")
        {
        }

        public IDbSet<Order> Orders {get;set;}
        public IDbSet<OrderDetail> OrderDetails { get; set; }
    }

Please guide me.

Thank you.

1
2
2/9/2017 2:36:03 PM

Accepted Answer

The injection site requires an EntitiesContext, but you only registered it for IdentityDbContext<ApplicationUser>.

Try this:

    builder.RegisterType<EntitiesContext>().AsSelf()
           .As<IdentityDbContext<ApplicationUser>>().InstancePerApiRequest();
5
2/9/2017 2:54:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow