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

pbhalchandra picture pbhalchandra · Jul 15, 2015 · Viewed 37.2k times · Source

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'LMS.Services.Security.EncryptionService' can be invoked with the available services and parameters: Cannot resolve parameter 'LMS.Models.SecuritySettings securitySettings' of constructor 'Void .ctor(LMS.Models.SecuritySettings)'

Here are the code files

Service Class

public class EncryptionService : IEncryptionService
{
    private readonly SecuritySettings _securitySettings;
    public EncryptionService(SecuritySettings securitySettings)
    {
        this._securitySettings = securitySettings;
    }
}

Bootstrapper

private static void SetAutofacContainer()
{
    var builder = new ContainerBuilder();
    builder.RegisterControllers(Assembly.GetExecutingAssembly());
    builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
    builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest();

    builder.RegisterAssemblyTypes(typeof(CourseRepository).Assembly)
           .Where(t => t.Name.EndsWith("Repository"))
           .AsImplementedInterfaces()
           .InstancePerRequest();

    builder.RegisterAssemblyTypes(typeof(CourseService).Assembly)
           .Where(t => t.Name.EndsWith("Service"))
           .AsImplementedInterfaces()
           .InstancePerRequest();

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

It was working earlier. But when I introduced the EncryptionService implementation, I am receiving above error. Here is the other working code implementation as follows

public class CourseService : ICourseService
{
    #region Fields

    private readonly IRepository<Course> _courseRepository;
    private readonly IUnitOfWork _unitOfWork;

    #endregion

    #region ctor

    public CourseService(IRepository<Course> courseRepository, IUnitOfWork unitOfWork)
    {
        _courseRepository = courseRepository;
        _unitOfWork = unitOfWork;
    }
    #endregion
}

Answer

Cyril Durand picture Cyril Durand · Jul 15, 2015

When Autofac try to resolve EncryptionService it tries to resolve a SecuritySettings service but Autofac is not aware of such a registration.

To resolve this error, you should register a SecuritySettings implementation.

For example :

builder.RegisterType<SecuritySettings>()
       .As<SecuritySettings>();