我有一个庞大的.net核心项目,使用依赖注入将很多类注入了解决方案的不同部分。
同时使用hangfire添加新的后台服务。由于某些原因,UserStore依赖关系变为空。
如果我评论后台服务,一切正常。
我已经尝试通过以下方式注入那些依赖项。
1)构造函数
2)服务提供商
3)将其注入Startup的configure方法中。然后提供给Hangfire经常性工作。
这些依赖项在注入控制器时可以很好地加载,但是如果我在后台服务或启动的configure方法中将它们注入,则会失败。
我也检查过,没有循环依赖性。
我正在使用基本的.net核心提供的依赖项注入。
此外,如果有一些常见的依赖注入技巧和最佳实践,请告诉我。
后台服务
如果我评论ISupervisorServices和IEmployeeServices,那么它将起作用。
此外,这两个服务也可在多个控制器和类中使用,没有任何问题。
但是在后台服务类或Startup的configure中使用它们会引发异常。
public class EmployeeNotificationJob : IEmployeeNotificationJob
{
private readonly IEmployeeServices _employeeServices;
private readonly ISupervisorServices _supervisorServices;
private readonly ITemplateServices _templateServices;
private readonly IEmailService _emailService;
public EmployeeNotificationJob(
IEmployeeServices employeeServices,
ISupervisorServices supervisorServices,
ITemplateServices templateServices,
IEmailService emailService)
{
_employeeServices = employeeServices;
_supervisorServices = supervisorServices;
_templateServices = templateServices;
_emailService = emailService;
}
public async Task Run()
{
我有一个庞大的.net核心项目,使用依赖项注入将许多类注入解决方案的不同部分。同时使用hangfire添加新的后台服务。 UserStore依赖项为...
似乎您应该在Hangfire中使用JobActivators来使用IoC。检查文档:https://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html