如何解决依赖注入Quartz.Net Scheduler
我目前正尝试使用存储库通过quartz.net更新数据库中的某些数据。 请记住,我正在使用ASP.Net Core 3.1
我目前遇到的问题是,当我在IJob的构造函数中注入IUserProjectRepository时,该作业将无法执行,并且在Quartz DB实现中也会出错:
所以,这就是我的startup.cs
的样子:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<UserProjectStatusJob>();
services.AddTransient(provider => GetScheduler().Result);
}
....
private async Task<IScheduler> GetScheduler()
{
NameValueCollection properties = new NameValueCollection
{
{ "quartz.scheduler.instanceName","Cliche" },{ "quartz.scheduler.instanceId",{ "quartz.jobStore.type","Quartz.Impl.AdoJobStore.JobStoreTX,Quartz" },{ "quartz.jobStore.useProperties","true" },{ "quartz.jobStore.dataSource","default" },{ "quartz.jobStore.tablePrefix","QRTZ_" },{
"quartz.dataSource.default.connectionString","connectionstring"
},{ "quartz.dataSource.default.provider","SqlServer" },{ "quartz.threadPool.threadCount","1" },{ "quartz.serializer.type","json" },};
var schedulerFactory = new StdSchedulerFactory(properties);
var scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();
return scheduler;
}
这是我的工作(UserProjectStatusJob
)的样子:
public class UserProjectStatusJob : IJob
{
private IUserProjectRepository _userProjectRepository;
public UserProjectStatusJob(IUserProjectRepository userProjectRepository)
{
this._userProjectRepository = userProjectRepository;
}
public Task Execute(IJobExecutionContext context)
{
try
{
JobDataMap dataMap = context.JobDetail.JobDataMap;
string userProjectId = dataMap.GetString("userProjectId");
string userProjectProjectId = dataMap.GetString("userProjectProjectId");
_userProjectRepository.CloseUserProject(userProjectProjectId,userProjectId);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return Task.FromResult(0);
}
}
我在同一UserProjectRepository
中创建我的工作:
public class UserProjectRepository : IUserProjectRepository
{
private readonly ApplicationDbContext _dbContext;
private readonly IFileService _fileService;
private readonly INotificationRepository _notificationRepository;
private readonly IScheduler _scheduler;
public UserProjectRepository(ApplicationDbContext dbContext,IFileService fileService,INotificationRepository notificationRepository,IScheduler scheduler)
{
this._scheduler = scheduler;
this._notificationRepository = notificationRepository;
this._fileService = fileService;
this._dbContext = dbContext;
}
public async Task CreateCronJobForUserProject(UserProject userProject)
{
// Add Later in to startAt
TimeSpan timeToTrigger = userProject.Project.Assignment.DeadLine - DateTime.Now;
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity($"Check Availability-{DateTime.Now}")
.StartAt(DateTime.Now.AddSeconds(15))
.WithPriority(1)
.Build();
IDictionary<string,object> map = new Dictionary<string,object>()
{
{"userProjectId",$"{userProject.Id}" },{"userProjectProjectId",$"{userProject.ProjectId}" },};
IJobDetail job = JobBuilder.Create<UserProjectStatusJob>()
.WithIdentity($"Check Availability-{DateTime.Now}")
.SetJobData(new JobDataMap(map))
.Build();
await this._scheduler.ScheduleJob(job,trigger);
}
}
编辑:
错误: 仔细查看后,我确实发现了这一点:
[14:46:50 ERR] An error occurred instantiating job to be executed. job= 'DEFAULT.Check Availability-10/28/2020 14:46:35'
Quartz.SchedulerException: Problem instantiating class 'IKL.Data.Services.UserProjectStatusJob: Cannot instantiate type which has no empty constructor (Parameter 'UserProjectStatusJob')'
---> System.ArgumentException: Cannot instantiate type which has no empty constructor (Parameter 'UserProjectStatusJob')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。