问题描述
我目前正尝试使用存储库通过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 (将#修改为@)