Asp.net core 依赖项注入.Net调度程序
我目前正在尝试使用quartz.net使用存储库更新数据库中的一些数据。 请记住,我使用的是ASP.NETCore3.1 我目前面临的问题是,当我在IJob的构造函数中注入我的IUserProjectRepository时,作业将无法执行,并且在Quartz DB实现中也会出现错误: 这就是我的Asp.net core 依赖项注入.Net调度程序,asp.net-core,dependency-injection,quartz.net,Asp.net Core,Dependency Injection,Quartz.net,我目前正在尝试使用quartz.net使用存储库更新数据库中的一些数据。 请记住,我使用的是ASP.NETCore3.1 我目前面临的问题是,当我在IJob的构造函数中注入我的IUserProjectRepository时,作业将无法执行,并且在Quartz DB实现中也会出现错误: 这就是我的startup.cs的样子: public void ConfigureServices(IServiceCollection services) { services.AddTr
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", "Cliche" },
{ "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;
}
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);
}
}
你有什么例外吗?如果是,请发布异常详细信息。不,不幸的是没有错误,作业没有执行,正如您在屏幕截图中看到的,触发器状态随后设置为错误。我编辑了我的帖子,我确实发现了一个错误。我找到了一个解决方案,我使用了Hangfire而不是Quartz…一个选择是使用官方的集成包来让注入工作,你有什么例外吗?如果是,请发布异常详细信息。不,不幸的是没有错误,作业没有执行,正如您在屏幕截图中看到的,触发器状态随后设置为错误。我编辑了我的帖子,我确实发现了一个错误。我找到了一个解决方案,我使用了Hangfire而不是Quartz…一个选择是使用官方的集成包来让注入工作正常,并且
[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')