[英]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.