Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
}
like image 849
pbhalchandra Avatar asked Jul 15 '15 14:07

pbhalchandra


2 Answers

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>(); 
like image 100
Cyril Durand Avatar answered Sep 18 '22 18:09

Cyril Durand


in my case i haven't registered the context. I registered the context and it worked for me

builder.RegisterType<JComDbEntities>().AsSelf().As<DbContext>().InstancePerLifetimeScope();
like image 25
Pranav Mishra Avatar answered Sep 17 '22 18:09

Pranav Mishra