using Autofac; using Autofac.Extras.DynamicProxy; using IRaCIS.Application.Interfaces; using IRaCIS.Application.Services; using IRaCIS.Core.API.Utility.AOP; using IRaCIS.Core.Application; using IRaCIS.Core.Application.AOP; using IRaCIS.Core.Application.BackGroundJob; using IRaCIS.Core.Infra.EFCore; using IRaCIS.Core.Infra.EFCore.AuthUser; using Microsoft.AspNetCore.Http; using Panda.DynamicWebApi; using System; using System.Linq; using System.Reflection; namespace IRaCIS.Core.API { // ReSharper disable once IdentifierTypo public class AutofacModuleSetup : Autofac.Module { protected override void Load(ContainerBuilder containerBuilder) { #region byzhouhang 20210917 此处注册泛型仓储 可以减少Domain层 和Infra.EFcore 两层 空的仓储接口定义和 仓储文件定义 containerBuilder.RegisterGeneric(typeof(Repository<>)) .As(typeof(IRepository<>)).InstancePerLifetimeScope();//注册泛型仓储 containerBuilder.RegisterGeneric(typeof(EFUnitOfWork<>)) .As(typeof(IEFUnitOfWork<>)).InstancePerLifetimeScope();//注册仓储 #endregion #region 指定控制器也由autofac 来进行实例获取 https://www.cnblogs.com/xwhqwer/p/15320838.html //获取所有控制器类型并使用属性注入 containerBuilder.RegisterAssemblyTypes(typeof(BaseService).Assembly) .Where(type => typeof(IDynamicWebApi).IsAssignableFrom(type)) .PropertiesAutowired(); //var controllerBaseType = typeof(ControllerBase); //containerBuilder.RegisterAssemblyTypes(typeof(BaseService).Assembly) // .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType) // .PropertiesAutowired(); #endregion Assembly application = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "IRaCIS.Core.Application.dll"); containerBuilder.RegisterAssemblyTypes(application).Where(t => t.FullName.Contains("Service")) .PropertiesAutowired().AsImplementedInterfaces().EnableClassInterceptors(); Assembly infrastructure = Assembly.Load("IRaCIS.Core.Infra.EFCore"); containerBuilder.RegisterAssemblyTypes(infrastructure).AsImplementedInterfaces(); containerBuilder.RegisterType().As().SingleInstance(); containerBuilder.RegisterType().SingleInstance(); //Autofac 注册拦截器 需要注意的是生成api上服务上的动态代理AOP失效 间接掉用不影响 containerBuilder.RegisterType(); containerBuilder.RegisterType(); //containerBuilder.RegisterType(); //containerBuilder.RegisterType().As().SingleInstance(); //注册hangfire任务 依赖注入 containerBuilder.RegisterType().As().InstancePerDependency(); } } }