Ole*_*leg 3 .net asp.net-core-mvc hangfire
我的.NET Core项目使用的是Hangfire 1.6.4。
这是我的Schedule控制器:
[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);
}
Run Code Online (Sandbox Code Playgroud)
但是作业执行时出现错误:
// Job ID: #183
using MyProject.Services.Email;
var emailScheduler = Activate<EmailScheduler>();
await emailScheduler.ScheduleEmail(
"",
"myEmail@gmail.com",
FromJson<IEnumerable`1>("[]")
storesId
);
Run Code Online (Sandbox Code Playgroud)
System.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);
}
}
Run Code Online (Sandbox Code Playgroud)
在启动类中:
GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(services.BuildServiceProvider()));
Run Code Online (Sandbox Code Playgroud)
怎么了?
谢谢!
这对我有帮助:1.像这样注册Hangfire:
services.AddHangfire(configuration => configuration
.UseSqlServerStorage("connection string here"));
Run Code Online (Sandbox Code Playgroud)
2.还有一件事(应该是第一件事):
services.AddScoped<EmailScheduler, EmailScheduler>();
Run Code Online (Sandbox Code Playgroud)
一段时间以来,我没有将此答案标记为正确的答案。但是如果没有其他答案,我会这样做)
| 归档时间: |
|
| 查看次数: |
1235 次 |
| 最近记录: |