-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
SchedulerBuilder.cs
57 lines (48 loc) · 1.68 KB
/
SchedulerBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
namespace CronScheduler.AspNetCore
{
public class SchedulerBuilder
{
/// <summary>
/// EventHanlder for Startup for Hosted Apps.
/// </summary>
public EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskExceptionHandler = null;
public SchedulerBuilder(IServiceCollection services)
{
Services = services;
}
/// <summary>
/// <see cref="IServiceCollection"/> for the DI.
/// </summary>
public IServiceCollection Services { get; }
public IServiceCollection AddJob<TJob>()
where TJob: class, IScheduledJob
{
Services.AddSingleton<IScheduledJob,TJob>();
return Services;
}
public IServiceCollection AddJob<TJob>(Func<IServiceProvider, TJob> factory)
where TJob: class, IScheduledJob
{
Services.AddSingleton(typeof(IScheduledJob), factory);
return Services;
}
/// <summary>
/// Adds <see cref="IScheduledJob"/> to DI.
/// </summary>
/// <typeparam name="TJob"></typeparam>
/// <typeparam name="TJobOptions"></typeparam>
/// <param name="sectionName"></param>
/// <returns></returns>
public IServiceCollection AddJob<TJob,TJobOptions>(string sectionName = "SchedulerJobs")
where TJob: class, IScheduledJob
where TJobOptions: SchedulerOptions, new()
{
Services.Configure<TJob, TJobOptions>(sectionName);
AddJob<TJob>();
return Services;
}
}
}