using Autofac; 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; public class ContainerConfig { public static IContainer BuildContainer() { var builder = new ContainerBuilder(); var appAssemblys = StoreAppTypeFinder.Current.GetAssemblies().ToArray(); var connection = ConfigurationManager.ConnectionStrings["ConnKJD"]; if (connection == null || string.IsNullOrWhiteSpace(connection.ConnectionString)) { throw new ConfigurationErrorsException("未配置连接字符串:ConnKJD"); } // ImageCacheService 使用独立的 imagecache 数据库。 builder.Register(_ => { return new FreeSqlBuilder() .UseConnectionString( DataType.MySql, connection.ConnectionString) .Build(); }) .As() .SingleInstance(); // 注册 Web API 所有 Controller builder.RegisterApiControllers(appAssemblys); builder.RegisterInstance(NLogger.Instance).As().SingleInstance(); #region 注册所有IOC接口 Type typeSingleton = typeof(ISingletonDependency); builder.RegisterAssemblyTypes(appAssemblys) .Where(a => typeSingleton.IsAssignableFrom(a) && a.IsClass && !a.IsAbstract ) .AsImplementedInterfaces() .AsSelf() .SingleInstance(); Type typeTransient = typeof(ITransientDependency); builder.RegisterAssemblyTypes(appAssemblys) .Where(a => typeTransient.IsAssignableFrom(a) && a.IsClass && !a.IsAbstract ) .AsImplementedInterfaces() .AsSelf() .InstancePerDependency(); Type typeScoped = typeof(IScopedDependency); builder.RegisterAssemblyTypes(appAssemblys) .Where(a => typeScoped.IsAssignableFrom(a) && a.IsClass && !a.IsAbstract) .AsImplementedInterfaces() .AsSelf() .InstancePerRequest(); #endregion IServiceRegistrar registrar = new ServiceRegistrarProxy(builder); BuildHangfire(appAssemblys); IEnumerable startupList = ReflectionHelper.GetInstancesByInterface(appAssemblys); foreach (IStartup item in startupList.OrderByDescending(a => a.Priority)) { Debug.WriteLine($"Startup : {item.GetType()}"); item.Initialize(); item.Register(registrar); } var app = builder.Build(); 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(appAssemblys) .Where(t => typeof(IScheduledJobs).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract) .Select(t => new { Type = t, Attr = t.GetCustomAttribute() }) .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"), "* * * * * *"); } }