简体   繁体   English

Autofac没有使用'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'找到的构造函数

[英]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)' 使用可用的服务和参数可以调用在'LMS.Services.Security.EncryptionService'类型上找到'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'的构造函数:无法解析参数'LMS.Models.SecuritySettings securitySettings'的构造函数'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. 但是当我介绍EncryptionService实现时,我收到了上述错误。 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. Autofac尝试解析EncryptionService它会尝试解析SecuritySettings服务,但Autofac不知道这样的注册。

To resolve this error, you should register a SecuritySettings implementation. 要解决此错误,您应该注册SecuritySettings实现。

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 ):- 您还可以调整Autofac的行为,你原先预期的工作[并与一些其它容器的默认对齐]通过增加AnyConcreteTypeNotAlreadyRegisteredSource (见的来源的文档 ): -

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. 我已经将这个方案与委托工厂隐式关系类型一起使用,几乎从一系列应用程序中删除显式注册,但是因为你似乎已经走上了显式(boilerplaty:P)注册的道路,我建议使用googling AnyConcreteTypeNotAlreadyRegisteredSource来查看更广泛的计划是否适合您正在寻找的更好的方案。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有在类型上使用&#39;Autofac.Core.Activators.Reflection.DefaultConstructorFinder&#39;找到的构造函数 - None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 没有找到带有“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数 - None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' 使用 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder 找不到构造函数 - None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder MVC&#39;Autofac.Core.Activators.Reflection.DefaultConstructorFinder&#39;中找不到任何构造函数 - MVC None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' 使用'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'找到的构造函数都没有 - None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' DependencyResolutionException:没有找到类型为“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数 - DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type nopCommerce 项目中的 Autofac 异常(Autofac.Core.Activators.Reflection.NoConstructorsFoundException: 'No accessible constructors were found ) - Autofac exception in nopCommerce project (Autofac.Core.Activators.Reflection.NoConstructorsFoundException: 'No accessible constructors were found ) 在 Asp.NET Core 3.1 上注入依赖项时出现 Autofac.Core.Activators.Reflection.NoConstructorsFoundException - Autofac.Core.Activators.Reflection.NoConstructorsFoundException while injecting dependencies on Asp.NET Core 3.1 Autofac WCF反射注入 - Autofac WCF injection on reflection Autofac - 自动注册错误:没有&#39;公共绑定标志&#39;可以找到构造函数 - Autofac - auto registration error : No constructors can be found with 'Public binding flags'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM