using System; using System.Collections.Generic; using System.Linq; using System.Web; using Autofac; using System.Reflection; using Autofac.Core; using EMIS.DataLogic; using EMIS.Utility; using Bowin.Common.XML; using System.Xml.Linq; using EMIS.CommonLogic.SystemServices; using System.Configuration; using EMIS.ViewModel; namespace EMIS.CommonLogic { public class ServiceModule : Autofac.Module { private Dictionary iocConfigList; private Dictionary IocConfigList { get { if (iocConfigList == null) { string configPath = HttpContext.Current.Server.MapPath("~/Config/IOCConfig.xml"); XElement config = XElement.Load(new System.IO.FileStream(configPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read), LoadOptions.None); if (config.Elements().Count() == 0) { iocConfigList = new Dictionary(); return iocConfigList; } iocConfigList = (from x in config.Elements("component") select new { FullName = x.Attribute("name").GetStringValue(), Implement = x.Attribute("implement").GetStringValue() }).ToDictionary(x => x.FullName, x => x.Implement); } return iocConfigList; } } private Dictionary fileUploadConfigList; private Dictionary FileUploadConfigList { get { if (fileUploadConfigList == null) { var config = (FileUploadConfig)ConfigurationManager.GetSection("fileUploadConfig"); fileUploadConfigList = config.ServiceTypes; } return fileUploadConfigList; } } private string GetFileUploadServicesType(Type type) { if (FileUploadConfigList.ContainsKey(type.FullName)) { return FileUploadConfigList[type.FullName]; } return null; } protected override void Load(ContainerBuilder builder) { //通用类型注册 //情况1:注册没有被配置过的组件 var blAssembly = typeof(BaseServices).Assembly; builder.RegisterAssemblyTypes(blAssembly) .Where(x => x.FullName.StartsWith("EMIS.CommonLogic") && x.Name != "BaseServices" && x.Name.EndsWith("Services") && !x.GetInterfaces().Any(w => IocConfigList.ContainsKey(w.Namespace + "." + w.Name))) .AsImplementedInterfaces() .PropertiesAutowired() .InstancePerLifetimeScope(); var uploadFileServicesList = builder.RegisterTypes(blAssembly.GetTypes().Where(x => x.FullName.StartsWith("EMIS.CommonLogic") && x.Name != "BaseServices" && x.GetInterfaces().Any(w => w.Name == "IFileUploadServices" && w.Namespace == "EMIS.CommonLogic.SystemServices") && FileUploadConfigList.ContainsKey(x.FullName)).ToArray()); uploadFileServicesList.Named(GetFileUploadServicesType) .PropertiesAutowired() .InstancePerLifetimeScope(); //情况2:注册配置的组件; //如果后续有空,可以研究一下Autofac.Configuration.dll里面的读取配置的方式,如果成功可以简化掉这堆代码,调用的方式其实不难, //主要担心就是Autofac读取配置进行注册是会覆盖掉原有的设置,还是多注册一个映射类,我们需要的是覆盖掉的效果。 var configuratedComponentEnum = (from ic in IocConfigList select new { ic.Key, Value = ic.Value.Split(',') } into fic select new { Interface = fic.Key, Assembly = fic.Value[1].Trim(), Class = fic.Value[0].Trim() } into aic group aic by aic.Assembly into g select new { Assembly = g.Key, Classes = g.Select(x => new { x.Class, x.Interface }).ToList() }); var configuratedComponentList = configuratedComponentEnum.ToList(); var iblAssembly = typeof(IBaseServices).Assembly; foreach (var configuratedComponent in configuratedComponentList) { var assembly = Assembly.Load(configuratedComponent.Assembly); if (assembly == null) { continue; } foreach (var myClass in configuratedComponent.Classes) { var interfaceType = iblAssembly.GetType(myClass.Interface); var classType = assembly.GetType(myClass.Class); if (classType == null) { if (interfaceType != null) { builder.RegisterAssemblyTypes(blAssembly) .Where(x => x.GetInterfaces().Any(w => (w.Namespace + "." + w.Name) == myClass.Interface)) .As(interfaceType) .PropertiesAutowired() .InstancePerLifetimeScope(); } continue; } if (interfaceType != null) { builder.RegisterType(classType).As(interfaceType) .PropertiesAutowired() .InstancePerLifetimeScope(); } } } } } }