Files
SilkierQuartz/src/QuartzHostedService/IServiceCollectionExtensions.cs
2018-08-24 23:33:51 +05:00

34 lines
1.1 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
namespace QuartzHostedService
{
public static class IServiceCollectionExtensions
{
public static IJobRegistrator UseQuartzHostedService(this IServiceCollection services,
Action<NameValueCollection> stdSchedulerFactoryOptions)
{
services.AddHostedService<QuartzHostedService>();
services.AddTransient<ISchedulerFactory>(provider =>
{
var options = new NameValueCollection();
stdSchedulerFactoryOptions?.Invoke(options);
var result = new StdSchedulerFactory();
if (options.Count > 0)
result.Initialize(options);
return new StdSchedulerFactory();
});
services.AddTransient<IJobFactory, ServiceCollectionJobFactory>();
return new JobRegistrator(services);
}
}
}