AppServiceExtensions.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Attribute;
  2. using Base;
  3. using Services;
  4. using System.Reflection;
  5. namespace Infrastructure
  6. {
  7. /// <summary>
  8. /// App服务注册
  9. /// </summary>
  10. public static class AppServiceExtensions
  11. {
  12. /// <summary>
  13. /// 注册引用程序域中所有有AppService标记的类的服务
  14. /// </summary>
  15. /// <param name="services"></param>
  16. public static void AddAppService(this IServiceCollection services)
  17. {
  18. // var cls = AppSettings.Get<string[]>("InjectClass");
  19. // if (cls == null || cls.Length <= 0)
  20. // {
  21. // throw new Exception("请更新appsettings类");
  22. // }
  23. // foreach (var item in cls)
  24. // {
  25. // Register(services, item);
  26. // }
  27. //必须注册的service
  28. // services.AddTransient<ISysOperLogService, SysOperLogService>();
  29. services.AddTransient<ISysPermissionService, SysPermissionService>();
  30. services.AddTransient<ISysDeptService, SysDeptService>();
  31. services.AddTransient<ISysLoginService, SysLoginService>();
  32. services.AddTransient<ISysMenuService, SysMenuService>();
  33. services.AddTransient<ISysRoleMenuService, SysRoleMenuService>();
  34. services.AddTransient<ISysPostService, SysPostService>();
  35. services.AddTransient<ISysRoleService, SysRoleService>();
  36. services.AddTransient<ISysUserPostService, SysUserPostService>();
  37. services.AddTransient<ISysUserRoleService, SysUserRoleService>();
  38. services.AddTransient<ISysRoleDeptService, SysRoleDeptService>();
  39. services.AddTransient<ISysUserService, SysUserService>();
  40. services.AddTransient<ISysOauthClientDetailsService, SysOauthClientDetailsService>();
  41. services.AddTransient<IApiGroupService, ApiGroupService>();
  42. services.AddTransient<IApiInfoService, ApiInfoService>();
  43. services.AddTransient<ISysDictService, SysDictService>();
  44. services.AddTransient<ISysDictItemService, SysDictItemService>();
  45. services.AddTransient<IPriProjectService, PriProjectService>();
  46. services.AddTransient<IPriProjectParamService, PriProjectParamService>();
  47. services.AddTransient<IPriQueryTableService, PriQueryTableService>();
  48. services.AddTransient<IPriQueryFieldService, PriQueryFieldService>();
  49. services.AddTransient<IPriReturnFieldService, PriReturnFieldService>();
  50. services.AddTransient<IPriListService, PriListService>();
  51. services.AddTransient<IPriListAmountSetService, PriListAmountSetService>();
  52. services.AddTransient<IPriConditionService, PriConditionService>();
  53. services.AddTransient<IPriLoopSetService, PriLoopSetService>();
  54. services.AddTransient<IPriRecordService, PriRecordService>();
  55. services.AddTransient<IPriLogService, PriLogService>();
  56. services.AddTransient<IPriDatabaseSetService, PriDatabaseSetService>();
  57. services.AddTransient<IPriPrizeObjectFieldService, PriPrizeObjectFieldService>();
  58. services.AddTransient<IPriPrizeObjectTableService, PriPrizeObjectTableService>();
  59. services.AddTransient<IPriRequestLogService, PriRequestLogService>();
  60. services.AddTransient<IPriRecursionStartFieldService, PriRecursionStartFieldService>();
  61. services.AddTransient<IPriRecursionStartTableService, PriRecursionStartTableService>();
  62. services.AddTransient<IPriRecursionStartReturnFieldService, PriRecursionStartReturnFieldService>();
  63. services.AddTransient<IPriRecursionStartConditionService, PriRecursionStartConditionService>();
  64. services.AddTransient<IPriRecursionEndFieldService, PriRecursionEndFieldService>();
  65. services.AddTransient<IPriRecursionEndTableService, PriRecursionEndTableService>();
  66. services.AddTransient<IPriPrizeInFieldService, PriPrizeInFieldService>();
  67. services.AddTransient<IPriPrizeInTableService, PriPrizeInTableService>();
  68. services.AddTransient<IPriPrizeInQueryFieldService, PriPrizeInQueryFieldService>();
  69. services.AddTransient<IPriPrizeInSetService, PriPrizeInSetService>();
  70. }
  71. private static void Register(IServiceCollection services, string item)
  72. {
  73. Assembly assembly = Assembly.Load(item);
  74. foreach (var type in assembly.GetTypes())
  75. {
  76. var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
  77. if (serviceAttribute != null)
  78. {
  79. var serviceType = serviceAttribute.ServiceType;
  80. //情况1 适用于依赖抽象编程,注意这里只获取第一个
  81. if (serviceType == null && serviceAttribute.InterfaceServiceType)
  82. {
  83. serviceType = type.GetInterfaces().FirstOrDefault();
  84. }
  85. //情况2 不常见特殊情况下才会指定ServiceType,写起来麻烦
  86. if (serviceType == null)
  87. {
  88. serviceType = type;
  89. }
  90. switch (serviceAttribute.ServiceLifetime)
  91. {
  92. case LifeTime.Singleton:
  93. services.AddSingleton(serviceType, type);
  94. break;
  95. case LifeTime.Scoped:
  96. services.AddScoped(serviceType, type);
  97. break;
  98. case LifeTime.Transient:
  99. services.AddTransient(serviceType, type);
  100. break;
  101. default:
  102. services.AddTransient(serviceType, type);
  103. break;
  104. }
  105. //System.Console.WriteLine($"注册:{serviceType}");
  106. }
  107. }
  108. }
  109. }
  110. }