| | |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_QuartzJob.QuartzNet; |
| | | |
| | | namespace WIDESEAWCS_QuartzJob.QuartzExtensions |
| | | { |
| | |
| | | { |
| | | if (services == null) throw new ArgumentNullException(nameof(services)); |
| | | |
| | | services.AddSingleton<Storage>(); |
| | | services.AddSingleton<QuartzNetExtension>(); |
| | | //将JobFactory注入到服务中 |
| | | services.AddSingleton<IJobFactory, JobFactory>(); |
| | | //将SchedulerCenterServer注入到服务中 |
| | | services.AddSingleton<ISchedulerCenter, SchedulerCenterServer>(); |
| | | //任务注入 |
| | | var baseType = typeof(IJob); |
| | | //获取当前应用程序域的相对搜索路径 |
| | | var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory; |
| | | //获取指定路径下的所有程序集 |
| | | var referencedAssemblies = System.IO.Directory.GetFiles(path, "WIDESEAWCS_Tasks.dll").Select(Assembly.LoadFrom).ToArray(); |
| | | //获取所有定义的类型 |
| | | var types = referencedAssemblies |
| | | .SelectMany(a => a.DefinedTypes) |
| | | .Select(type => type.AsType()) |
| | | //筛选出继承自IJob的类型 |
| | | .Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray(); |
| | | //获取所有实现了IJob的类型 |
| | | var implementTypes = types.Where(x => x.IsClass).ToArray(); |
| | | //将所有实现了IJob的类型注入到服务中 |
| | | foreach (var implementType in implementTypes) |
| | | { |
| | | services.AddTransient(implementType); |