/// <summary> /// 统一注册MVC及服务程序集 /// </summary> /// <param name="containerBuilder">容器生成器</param> /// <param name="param">参数</param> /// <returns>容器</returns> public static IContainer UnifiedRegisterAssemblysForMvc5(this ContainerBuilder containerBuilder, WebBuilderParam param) { var assemblyList = new List <Assembly>(); foreach (BasicAssemblyInfo assembly in param.AssemblyControllers) { Assembly[] assemblies = AssemblyUtil.Load(assembly.Names); if (assemblies.IsNullOrLength0()) { return(null); } assemblyList.AddRange(assemblies); if (!assembly.InterceptedTypes.IsNullOrLength0()) { foreach (Type type in assembly.InterceptedTypes) { containerBuilder.RegisterType(type); } } if (assembly.Intercepteds.IsNullOrLength0()) { containerBuilder.RegisterControllers(assemblies) .PropertiesAutowired() .AsImplementedInterfaces() .Where(AutofacUtil.CanInject) .AsSelf(); } else { containerBuilder.RegisterControllers(assemblies) .PropertiesAutowired() .AsImplementedInterfaces() .AsSelf() .InterceptedBy(assembly.Intercepteds) .Where(AutofacUtil.CanInject) .EnableClassInterceptors(); } } if (param.RegisteringControllerAction != null) { param.RegisteringControllerAction(); } IContainer container = containerBuilder.UnifiedRegisterAssemblys(param); //将MVC的控制器对象实例 交由autofac来创建 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); WebAutofacTool.MvcDependencyResolver = DependencyResolver.Current; Hzdtf.Autofac.Extend.Standard.AutofacTool.ResolveFunc = WebAutofacTool.GetMvcService; if (param.IsLoadAutoMapperConfig) { AutoMapperUtil.AutoRegisterConfig(assemblyList.ToArray()); } return(container); }
/// <summary> /// 统一注册服务程序集 /// </summary> /// <param name="containerBuilder">容器生成器</param> /// <param name="param">参数</param> /// <param name="isExecBuilderContainer">是否执行生成容器,如果为false,则返回值为null</param> /// <returns>容器</returns> public static IContainer UnifiedRegisterAssemblys(this ContainerBuilder containerBuilder, BuilderParam param, bool isExecBuilderContainer = true) { var assemblyList = new List <Assembly>(); foreach (AssemblyExpandInfo assembly in param.AssemblyServices) { Assembly[] assemblies = AssemblyUtil.Load(assembly.Names); if (assemblies.IsNullOrLength0()) { return(null); } assemblyList.AddRange(assemblies); var registerBuilder = containerBuilder.RegisterAssemblyTypes(assemblies) .PropertiesAutowired() .AsImplementedInterfaces() .Where(AutofacUtil.CanInject) .AsSelf(); if (!assembly.InterceptedTypes.IsNullOrLength0()) { foreach (Type type in assembly.InterceptedTypes) { containerBuilder.RegisterType(type); } registerBuilder.InterceptedBy(assembly.InterceptedTypes).EnableClassInterceptors(); } switch (assembly.Lifecycle) { case LifecycleType.DEPENDENCY: registerBuilder.InstancePerDependency(); break; case LifecycleType.LIFETIME_SCOPE: registerBuilder.InstancePerLifetimeScope(); break; case LifecycleType.MATCH_LIFETIME_SCOPE: registerBuilder.InstancePerMatchingLifetimeScope(assembly.MatchTagNames); break; case LifecycleType.REQUEST: registerBuilder.InstancePerRequest(); break; case LifecycleType.SIGNLETON: registerBuilder.SingleInstance(); break; } } if (param.RegisteringServiceAction != null) { param.RegisteringServiceAction(); } if (isExecBuilderContainer) { AutofacTool.Container = containerBuilder.Build(); } else { containerBuilder.RegisterBuildCallback(scope => { AutofacTool.LifetimeScope = scope; }); } if (param.IsLoadAutoMapperConfig) { AutoMapperUtil.AutoRegisterConfig(assemblyList.ToArray()); } return(AutofacTool.Container); }