24

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
}
Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
pbhalchandra
  • 287
  • 1
  • 6
  • 14

3 Answers3

32

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>(); 
Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
3

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();
Pranav Mishra
  • 456
  • 2
  • 7
  • 14
2

You can also adjust Autofac's behavior to work as you originally anticipated [and align with the defaults of some other containers] by adding the AnyConcreteTypeNotAlreadyRegisteredSource (see the docs for Sources):-

var builder = new ContainerBuilder();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());

I've used this scheme together with delegate factories and implicit Relationship Types to pretty much remove explicit registration from a suite of apps but as you seem tohave gone down the road of explicit (boilerplaty :P) registration I'd recommend googling AnyConcreteTypeNotAlreadyRegisteredSource to see whether a broader scheme may fit what you're looking for better.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249