using Hangfire; using Microsoft.Extensions.Configuration; using System.Reflection; using JiShe.CollectBus.Common.Jobs; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions { /// /// 添加TcpService服务。 /// public static IServiceCollection AddJobs(this IServiceCollection services, IConfiguration configuration) { var interfaceType = typeof(IBaseJob); var assembly = Assembly.GetExecutingAssembly(); var types = assembly.GetTypes(); foreach (var type in types) { if (type.IsClass && type.GetInterfaces().Contains(interfaceType)) { var instance = services.GetServiceProviderOrNull().GetService(type); //var instance = services.GetObject(); //var instance = Activator.CreateInstance(type); RecurringJob.AddOrUpdate(type.Name, () => type.GetMethod("Execute").Invoke(instance,null), Cron.Minutely); } } return services; } } }