AppServiceExtensions.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. }
  46. private static void Register(IServiceCollection services, string item)
  47. {
  48. Assembly assembly = Assembly.Load(item);
  49. foreach (var type in assembly.GetTypes())
  50. {
  51. var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
  52. if (serviceAttribute != null)
  53. {
  54. var serviceType = serviceAttribute.ServiceType;
  55. //情况1 适用于依赖抽象编程,注意这里只获取第一个
  56. if (serviceType == null && serviceAttribute.InterfaceServiceType)
  57. {
  58. serviceType = type.GetInterfaces().FirstOrDefault();
  59. }
  60. //情况2 不常见特殊情况下才会指定ServiceType,写起来麻烦
  61. if (serviceType == null)
  62. {
  63. serviceType = type;
  64. }
  65. switch (serviceAttribute.ServiceLifetime)
  66. {
  67. case LifeTime.Singleton:
  68. services.AddSingleton(serviceType, type);
  69. break;
  70. case LifeTime.Scoped:
  71. services.AddScoped(serviceType, type);
  72. break;
  73. case LifeTime.Transient:
  74. services.AddTransient(serviceType, type);
  75. break;
  76. default:
  77. services.AddTransient(serviceType, type);
  78. break;
  79. }
  80. //System.Console.WriteLine($"注册:{serviceType}");
  81. }
  82. }
  83. }
  84. }
  85. }