AppServiceExtensions.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Attribute;
  2. using Base;
  3. using Services;
  4. using Services.Base;
  5. using System.Reflection;
  6. namespace Infrastructure
  7. {
  8. /// <summary>
  9. /// App服务注册
  10. /// </summary>
  11. public static class AppServiceExtensions
  12. {
  13. /// <summary>
  14. /// 注册引用程序域中所有有AppService标记的类的服务
  15. /// </summary>
  16. /// <param name="services"></param>
  17. public static void AddAppService(this IServiceCollection services)
  18. {
  19. // var cls = AppSettings.Get<string[]>("InjectClass");
  20. // if (cls == null || cls.Length <= 0)
  21. // {
  22. // throw new Exception("请更新appsettings类");
  23. // }
  24. // foreach (var item in cls)
  25. // {
  26. // Register(services, item);
  27. // }
  28. //必须注册的service
  29. // services.AddTransient<ISysOperLogService, SysOperLogService>();
  30. services.AddTransient<IAppReportRecordService, AppReportRecordService>();
  31. services.AddTransient<IAppSourceSetService, AppSourceSetService>();
  32. services.AddTransient<IAppSourceVersionService, AppSourceVersionService>();
  33. services.AddTransient<IAppVersionService, AppVersionService>();
  34. services.AddTransient<IAppBottomNavsService, AppBottomNavsService>();
  35. services.AddTransient<IPageUpdateInfoService, PageUpdateInfoService>();
  36. services.AddTransient<IFileUpdateInfoService, FileUpdateInfoService>();
  37. services.AddTransient<IOssService, OssService>();
  38. services.AddTransient<Services.Client.IAppBottomNavsService, Services.Client.AppBottomNavsService>();
  39. services.AddTransient<Services.Client.IPageUpdateInfoService, Services.Client.PageUpdateInfoService>();
  40. services.AddTransient<Services.Client.IFileUpdateInfoService, Services.Client.FileUpdateInfoService>();
  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. }