2024-11-13 00:30:24 +08:00

37 lines
1.2 KiB
C#

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)
{
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<IBaseJob>();
//var instance = Activator.CreateInstance(type);
RecurringJob.AddOrUpdate(type.Name, () => type.GetMethod("Execute").Invoke(instance,null), Cron.Minutely);
}
}
return services;
}
}
}