Не получается запустить Job в Quartz.net
Цель запустить job в Quartz в приложении .net core 3.1 c dependency injection. Беру самый простой пример с официального сайта . В итоге ставлю бп внутри joba результат нулевой. Не пойму что упустил.
startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddQuartz(q =>
{
q.ScheduleJob<ExampleJob>(trigger => trigger
.WithIdentity("Combined Configuration Trigger")
.StartAt(DateBuilder.EvenSecondDate(DateTimeOffset.UtcNow.AddSeconds(7)))
.WithDailyTimeIntervalSchedule(x => x.WithInterval(10, IntervalUnit.Second))
.WithDescription("my awesome trigger configured for a job with single call")
);
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
exampleJob.cs
public class ExampleJob : IJob
{
private readonly IApplicationBuilder _app;
public ExampleJob(IApplicationBuilder app)
{
_app = app;
}
public async Task Execute(IJobExecutionContext context)
{
_app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
Program.cs стандартный, ничего в нем не менял
Ответы (1 шт):
Автор решения: Vasiliy
→ Ссылка
Ответ найден. Оказывается нужно было добавить QuartzHostedService. А также обязательно зарегистрировать свой job в DI контейнере.
public static class Services
{
public static void ConfigureApplicationServices(this IServiceCollection services, IConfiguration configuration)
{
//services.Configure<QuartzOptions>(configuration.GetSection("Quartz"));
services.AddQuartz(q =>
{
q.ScheduleJob<ExampleJob>(trigger => trigger
.WithIdentity("Combined Configuration Trigger")
.StartAt(DateBuilder.EvenSecondDate(DateTimeOffset.UtcNow.AddSeconds(7)))
.WithDailyTimeIntervalSchedule(x => x.WithInterval(10, IntervalUnit.Second))
.WithDescription("my awesome trigger configured for a job with single call")
);
});
services.AddQuartzHostedService(options =>
{
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
services.AddTransient<ExampleJob>();
}
}