gpt4 book ai didi

.net - Hangfire 1.6.4 .NET Core - 无法解析依赖项

转载 作者:行者123 更新时间:2023-12-01 11:23:53 24 4
gpt4 key购买 nike

我正在为我的 .NET Core 项目使用 Hangfire 1.6.4。

这是我的日程 Controller :

    [HttpPost]
public void Post([FromBody]EmailSchedulerDto emailSchedulerDto)
{
// test only
emailSchedulerDto = new EmailSchedulerDto
{
UserId = "",
Email = "myEmail@gmail.com",
StoresId = new List<string>(),
CronPattern = "* * * * *"
};
RecurringJob.AddOrUpdate(
() =>
_emailScheduler.ScheduleEmail(emailSchedulerDto.UserId, emailSchedulerDto.Email,
emailSchedulerDto.StoresId), emailSchedulerDto.CronPattern);
}

但是我在作业执行过程中出现错误:

// Job ID: #183
using MyProject.Services.Email;

var emailScheduler = Activate<EmailScheduler>();
await emailScheduler.ScheduleEmail(
"",
"myEmail@gmail.com",
FromJson<IEnumerable`1>("[]")
storesId
);

系统.InvalidOperationException

尚未注册“MyProject.Services.Email.EmailScheduler”类型的服务。

我已经像这样注册了 ContainerJobActivator:

public class ContainerJobActivator : JobActivator
{
private readonly IServiceProvider _serviceProvider;

public ContainerJobActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public override object ActivateJob(Type type)
{
return _serviceProvider.GetService(type);
}
}

在 Startup 类中:

GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(services.BuildServiceProvider()));

怎么了?

谢谢!

最佳答案

以下是对我有帮助的内容:1. 像这样注册 Hangfire:

services.AddHangfire(configuration => configuration
.UseSqlServerStorage("connection string here"));

2。还有一件事(应该是第一件事):

services.AddScoped<EmailScheduler, EmailScheduler>();

我有一段时间没有将此答案标记为正确答案...但如果没有其他答案,我会做)

关于.net - Hangfire 1.6.4 .NET Core - 无法解析依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39357476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com