user2517610
user2517610

Reputation: 275

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

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.

Upvotes: 2

Views: 11651

Answers (1)

hoodaticus
hoodaticus

Reputation: 3880

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

Try this:

    builder.RegisterType<EntitiesContext>().AsSelf()
           .As<IdentityDbContext<ApplicationUser>>().InstancePerApiRequest();

Upvotes: 5

Related Questions