using Autofac; using Autofac.Extras.DynamicProxy; using IRaCIS.Core.Application; using IRaCIS.Core.Application.BackGroundJob; using IRaCIS.Core.Infra.EFCore; using Microsoft.AspNetCore.Http; using Panda.DynamicWebApi; using System; using System.Linq; using System.Reflection; using IRaCIS.Core.Domain.Models; using IRaCIS.Core.Domain.Share; using MediatR; using IRaCIS.Application.Services; using IRaCIS.Application.Interfaces; using AutoMapper; using Quartz; 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.RegisterType().As().InstancePerLifetimeScope(); //containerBuilder.RegisterType().As().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(); #endregion //containerBuilder.RegisterType().As().PropertiesAutowired().InstancePerLifetimeScope(); //containerBuilder.RegisterType().As().InstancePerLifetimeScope(); 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().As().InstancePerLifetimeScope(); //containerBuilder.RegisterType().InstancePerLifetimeScope(); //Autofac 注册拦截器 需要注意的是生成api上服务上的动态代理AOP失效 间接掉用不影响 //containerBuilder.RegisterType(); //containerBuilder.RegisterType(); //containerBuilder.RegisterType(); //containerBuilder.RegisterType().As().SingleInstance(); //注册hangfire任务 依赖注入 containerBuilder.RegisterType().As().InstancePerDependency(); } } }