增加定时任务,

This commit is contained in:
chfei committed 2026-09-30 15:46:20 +08:00
1 parent be1fc11e07
commit be08510a84
10 files changed
+171 -8

No files matched your search

@@ -2,19 +2,26 @@
using Autofac.Core;
using Autofac.Integration.WebApi;
using FreeSql;
using Hangfire;
using Hangfire.Common;
using Hangfire.InMemory;
using MAX.Core.DependencyInjection;
using MAX.Core.Helpers;
using MAX.Core.Interfaces;
using MAX.Core.Logger;
using MAX.Framework.Logger;
using Org.BouncyCastle.Utilities.Collections;
using Store.Framework.Commons;
using Store.Framework.Jobs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Dependencies;
namespace Store.Framework.DependencyInjection;
@@ -24,7 +31,7 @@ public class ContainerConfig
public static IContainer BuildContainer()
{
var builder = new ContainerBuilder();
var appAssembly = StoreAppTypeFinder.Current.GetAssemblies().ToArray();
var appAssemblys = StoreAppTypeFinder.Current.GetAssemblies().ToArray();
var connection = ConfigurationManager.ConnectionStrings["ConnKJD"];
if (connection == null || string.IsNullOrWhiteSpace(connection.ConnectionString))
@@ -45,7 +52,7 @@ public class ContainerConfig
// 注册 Web API 所有 Controller
builder.RegisterApiControllers(appAssembly);
builder.RegisterApiControllers(appAssemblys);
builder.RegisterInstance(NLogger.Instance).As<ILoggerFacade>().SingleInstance();
@@ -54,7 +61,7 @@ public class ContainerConfig
#region 注册所有IOC接口
Type typeSingleton = typeof(ISingletonDependency);
builder.RegisterAssemblyTypes(appAssembly)
builder.RegisterAssemblyTypes(appAssemblys)
.Where(a =>
typeSingleton.IsAssignableFrom(a)
&& a.IsClass
@@ -65,7 +72,7 @@ public class ContainerConfig
.SingleInstance();
Type typeTransient = typeof(ITransientDependency);
builder.RegisterAssemblyTypes(appAssembly)
builder.RegisterAssemblyTypes(appAssemblys)
.Where(a =>
typeTransient.IsAssignableFrom(a)
&& a.IsClass
@@ -76,7 +83,7 @@ public class ContainerConfig
.InstancePerDependency();
Type typeScoped = typeof(IScopedDependency);
builder.RegisterAssemblyTypes(appAssembly)
builder.RegisterAssemblyTypes(appAssemblys)
.Where(a => typeScoped.IsAssignableFrom(a) && a.IsClass && !a.IsAbstract)
.AsImplementedInterfaces()
.AsSelf()
@@ -84,7 +91,13 @@ public class ContainerConfig
#endregion
IServiceRegistrar registrar = new ServiceRegistrarProxy(builder);
IEnumerable<IStartup> startupList = ReflectionHelper.GetInstancesByInterface<IStartup>(appAssembly);
BuildHangfire(appAssemblys);
IEnumerable<IStartup> startupList = ReflectionHelper.GetInstancesByInterface<IStartup>(appAssemblys);
foreach (IStartup item in startupList.OrderByDescending(a => a.Priority))
{
Debug.WriteLine($"Startup : {item.GetType()}");
@@ -96,4 +109,40 @@ public class ContainerConfig
return app;
}
public static void BuildHangfire(IDependencyResolver app)
{
Hangfire.GlobalConfiguration.Configuration
.UseInMemoryStorage()
.UseActivator(new ContainerJobActivator(app));
var server = new Hangfire.BackgroundJobServer();
var appAssemblys = StoreAppTypeFinder.Current.GetAssemblies().ToArray();
var runAsync = typeof(IScheduledJobs).GetMethod(nameof(IScheduledJobs.RunAsync));
var jobTypes = ReflectionHelper.FindTypes<IScheduledJobs>(appAssemblys)
.Where(t => typeof(IScheduledJobs).IsAssignableFrom(t)
&& !t.IsInterface
&& !t.IsAbstract)
.Select(t => new { Type = t, Attr = t.GetCustomAttribute<JobAttribute>() })
.Where(x => x.Attr != null);
var manager = new RecurringJobManager();
foreach (var x in jobTypes)
{
var job = new Job(x.Type, runAsync);
manager.AddOrUpdate(
x.Attr.Name,
job,
x.Attr.Cron
);
}
}
private static void BuildHangfire(Assembly[] assemblies)
{
//Hangfire.RecurringJob.AddOrUpdate("synclogistics_balance_check", ()=>Debug.WriteLine("1"), "* * * * * *");
}
}