2024-11-12 18:18:43 +08:00
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加TcpService服务。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static IServiceCollection AddJobs(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
|
{
|
2024-11-13 00:30:24 +08:00
|
|
|
|
|
2024-11-12 18:18:43 +08:00
|
|
|
|
var interfaceType = typeof(IBaseJob);
|
|
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
|
|
|
|
|
|
|
var types = assembly.GetTypes();
|
|
|
|
|
|
foreach (var type in types)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (type.IsClass && type.GetInterfaces().Contains(interfaceType))
|
|
|
|
|
|
{
|
2024-11-13 00:30:24 +08:00
|
|
|
|
var instance = services.GetServiceProviderOrNull().GetService(type);
|
|
|
|
|
|
|
|
|
|
|
|
//var instance = services.GetObject<IBaseJob>();
|
|
|
|
|
|
|
|
|
|
|
|
//var instance = Activator.CreateInstance(type);
|
|
|
|
|
|
RecurringJob.AddOrUpdate(type.Name, () => type.GetMethod("Execute").Invoke(instance,null), Cron.Minutely);
|
2024-11-12 18:18:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|