/// <summary> /// 注册AutoConfiguration /// </summary> /// <param name="builder"></param> /// <param name="list"></param> public static void Register(ContainerBuilder builder, AutoConfigurationList list) { //注册Configuration foreach (var configuration in list.AutoConfigurationDetailList) { RegisterConfiguration(builder, configuration); } }
private void DoAutofacConfiguration(ContainerBuilder builder) { AutoConfigurationList list = new AutoConfigurationList { AutoConfigurationDetailList = new List <AutoConfigurationDetail>() }; try { var allConfiguration = GetAllAutofacConfiguration(); if (!allConfiguration.Any()) { return; } if (!string.IsNullOrEmpty(this.AutofacConfigurationKey)) { allConfiguration = allConfiguration.Where(r => r.Key.Equals(this.AutofacConfigurationKey)).ToList(); } foreach (var configuration in allConfiguration) { var beanTypeMethodList = configuration.Type.GetAllInstanceMethod(false); var bean = new AutoConfigurationDetail { AutoConfigurationClassType = configuration.Type, BeanMethodInfoList = new List <Tuple <Bean, MethodInfo> >(), MetaSourceDataList = new List <MetaSourceData>() }; foreach (var beanTypeMethod in beanTypeMethodList) { var beanAttribute = beanTypeMethod.GetCustomAttribute <Bean>(); if (beanAttribute == null) { continue; } bean.BeanMethodInfoList.Add(new Tuple <Bean, MethodInfo>(beanAttribute, beanTypeMethod)); } builder.RegisterType(configuration.Type).AsSelf().SingleInstance();//注册为单例模式 list.AutoConfigurationDetailList.Add(bean); EnumerateMetaSourceAttributes(bean.AutoConfigurationClassType, bean.MetaSourceDataList); } } finally { builder.RegisterInstance(list).SingleInstance(); } }