简体   繁体   中英

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
}

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>(); 

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM