AppServiceExtensions.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // services.AddTransient<ISysOperLogService, SysOperLogService>();
  28. services.AddTransient<ISysPermissionService, SysPermissionService>();
  29. services.AddTransient<ISysDeptService, SysDeptService>();
  30. services.AddTransient<ISysLoginService, SysLoginService>();
  31. services.AddTransient<ISysMenuService, SysMenuService>();
  32. services.AddTransient<ISysRoleMenuService, SysRoleMenuService>();
  33. services.AddTransient<ISysPostService, SysPostService>();
  34. services.AddTransient<ISysRoleService, SysRoleService>();
  35. services.AddTransient<ISysUserPostService, SysUserPostService>();
  36. services.AddTransient<ISysUserRoleService, SysUserRoleService>();
  37. services.AddTransient<ISysRoleDeptService, SysRoleDeptService>();
  38. services.AddTransient<ISysUserService, SysUserService>();
  39. services.AddTransient<ISysOauthClientDetailsService, SysOauthClientDetailsService>();
  40. // services.AddTransient<IApiGroupService, ApiGroupService>();
  41. }
  42. private static void Register(IServiceCollection services, string item)
  43. {
  44. Assembly assembly = Assembly.Load(item);
  45. foreach (var type in assembly.GetTypes())
  46. {
  47. var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
  48. if (serviceAttribute != null)
  49. {
  50. var serviceType = serviceAttribute.ServiceType;
  51. //情况1 适用于依赖抽象编程,注意这里只获取第一个
  52. if (serviceType == null && serviceAttribute.InterfaceServiceType)
  53. {
  54. serviceType = type.GetInterfaces().FirstOrDefault();
  55. }
  56. //情况2 不常见特殊情况下才会指定ServiceType,写起来麻烦
  57. if (serviceType == null)
  58. {
  59. serviceType = type;
  60. }
  61. switch (serviceAttribute.ServiceLifetime)
  62. {
  63. case LifeTime.Singleton:
  64. services.AddSingleton(serviceType, type);
  65. break;
  66. case LifeTime.Scoped:
  67. services.AddScoped(serviceType, type);
  68. break;
  69. case LifeTime.Transient:
  70. services.AddTransient(serviceType, type);
  71. break;
  72. default:
  73. services.AddTransient(serviceType, type);
  74. break;
  75. }
  76. //System.Console.WriteLine($"注册:{serviceType}");
  77. }
  78. }
  79. }
  80. }
  81. }