using EntrustSettle.Common; using EntrustSettle.Tasks; using Microsoft.Extensions.DependencyInjection; using Quartz; using Quartz.Spi; using System; using System.IO; using System.Linq; using System.Reflection; namespace EntrustSettle.Extensions { /// /// 任务调度 启动服务 /// public static class JobSetup { public static void AddJobSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); // 简易版的定时任务 if (AppSettings.app("TimedJob", "Enabled").ObjToBool()) { services.AddHostedService(); } // Quartz版的定时任务 if (AppSettings.app("QuartzNetJob", "Enabled").ObjToBool()) { services.AddSingleton(); //services.AddTransient(); //Job使用瞬时依赖注入 services.AddSingleton(); } //批量注入Quartz版的定时任务 var baseType = typeof(IJob); var pith = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; var assemblie = Assembly.LoadFrom(Path.Combine(pith, "EntrustSettle.Tasks.dll")); var implementTypes = assemblie.GetTypes() .Where(t => baseType.IsAssignableFrom(t) && t.IsClass) .ToArray(); foreach (var implementType in implementTypes) { services.AddTransient(implementType); } } } }