ServiceModule.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Autofac;
  6. using System.Reflection;
  7. using Autofac.Core;
  8. using EMIS.DataLogic;
  9. using EMIS.Utility;
  10. using Bowin.Common.XML;
  11. using System.Xml.Linq;
  12. using EMIS.CommonLogic.SystemServices;
  13. using System.Configuration;
  14. using EMIS.ViewModel;
  15. namespace EMIS.CommonLogic
  16. {
  17. public class ServiceModule : Autofac.Module
  18. {
  19. private Dictionary<string, string> iocConfigList;
  20. private Dictionary<string, string> IocConfigList
  21. {
  22. get
  23. {
  24. if (iocConfigList == null)
  25. {
  26. string configPath = HttpContext.Current.Server.MapPath("~/Config/IOCConfig.xml");
  27. XElement config = XElement.Load(new System.IO.FileStream(configPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read), LoadOptions.None);
  28. if (config.Elements().Count() == 0)
  29. {
  30. iocConfigList = new Dictionary<string, string>();
  31. return iocConfigList;
  32. }
  33. iocConfigList = (from x in config.Elements("component")
  34. select new
  35. {
  36. FullName = x.Attribute("name").GetStringValue(),
  37. Implement = x.Attribute("implement").GetStringValue()
  38. }).ToDictionary(x => x.FullName, x => x.Implement);
  39. }
  40. return iocConfigList;
  41. }
  42. }
  43. private Dictionary<string, string> fileUploadConfigList;
  44. private Dictionary<string, string> FileUploadConfigList
  45. {
  46. get
  47. {
  48. if (fileUploadConfigList == null)
  49. {
  50. var config = (FileUploadConfig)ConfigurationManager.GetSection("fileUploadConfig");
  51. fileUploadConfigList = config.ServiceTypes;
  52. }
  53. return fileUploadConfigList;
  54. }
  55. }
  56. private string GetFileUploadServicesType(Type type)
  57. {
  58. if (FileUploadConfigList.ContainsKey(type.FullName))
  59. {
  60. return FileUploadConfigList[type.FullName];
  61. }
  62. return null;
  63. }
  64. protected override void Load(ContainerBuilder builder)
  65. {
  66. //通用类型注册
  67. //情况1:注册没有被配置过的组件
  68. var blAssembly = typeof(BaseServices).Assembly;
  69. builder.RegisterAssemblyTypes(blAssembly)
  70. .Where(x => x.FullName.StartsWith("EMIS.CommonLogic") &&
  71. x.Name != "BaseServices" &&
  72. x.Name.EndsWith("Services") && !x.GetInterfaces().Any(w => IocConfigList.ContainsKey(w.Namespace + "." + w.Name)))
  73. .AsImplementedInterfaces()
  74. .PropertiesAutowired()
  75. .InstancePerLifetimeScope();
  76. var uploadFileServicesList = builder.RegisterTypes(blAssembly.GetTypes().Where(x => x.FullName.StartsWith("EMIS.CommonLogic") &&
  77. x.Name != "BaseServices" && x.GetInterfaces().Any(w => w.Name == "IFileUploadServices" && w.Namespace == "EMIS.CommonLogic.SystemServices")
  78. && FileUploadConfigList.ContainsKey(x.FullName)).ToArray());
  79. uploadFileServicesList.Named<IFileUploadServices>(GetFileUploadServicesType)
  80. .PropertiesAutowired()
  81. .InstancePerLifetimeScope();
  82. //情况2:注册配置的组件;
  83. //如果后续有空,可以研究一下Autofac.Configuration.dll里面的读取配置的方式,如果成功可以简化掉这堆代码,调用的方式其实不难,
  84. //主要担心就是Autofac读取配置进行注册是会覆盖掉原有的设置,还是多注册一个映射类,我们需要的是覆盖掉的效果。
  85. var configuratedComponentEnum = (from ic in IocConfigList
  86. select new { ic.Key, Value = ic.Value.Split(',') } into fic
  87. select new { Interface = fic.Key, Assembly = fic.Value[1].Trim(), Class = fic.Value[0].Trim() } into aic
  88. group aic by aic.Assembly into g
  89. select new { Assembly = g.Key, Classes = g.Select(x => new { x.Class, x.Interface }).ToList() });
  90. var configuratedComponentList = configuratedComponentEnum.ToList();
  91. var iblAssembly = typeof(IBaseServices).Assembly;
  92. foreach (var configuratedComponent in configuratedComponentList)
  93. {
  94. var assembly = Assembly.Load(configuratedComponent.Assembly);
  95. if (assembly == null)
  96. {
  97. continue;
  98. }
  99. foreach (var myClass in configuratedComponent.Classes)
  100. {
  101. var interfaceType = iblAssembly.GetType(myClass.Interface);
  102. var classType = assembly.GetType(myClass.Class);
  103. if (classType == null)
  104. {
  105. if (interfaceType != null)
  106. {
  107. builder.RegisterAssemblyTypes(blAssembly)
  108. .Where(x => x.GetInterfaces().Any(w => (w.Namespace + "." + w.Name) == myClass.Interface))
  109. .As(interfaceType)
  110. .PropertiesAutowired()
  111. .InstancePerLifetimeScope();
  112. }
  113. continue;
  114. }
  115. if (interfaceType != null)
  116. {
  117. builder.RegisterType(classType).As(interfaceType)
  118. .PropertiesAutowired()
  119. .InstancePerLifetimeScope();
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }