ServiceCollectionExtensions.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Masuit.Tools.AspNetCore.ResumeFileResults.Executor;
  2. using Masuit.Tools.AspNetCore.ResumeFileResults.ResumeFileResult;
  3. using Masuit.Tools.Files;
  4. using Microsoft.AspNetCore.Mvc.Infrastructure;
  5. using Microsoft.Extensions.DependencyInjection.Extensions;
  6. using System.Reflection;
  7. namespace Masuit.Tools.Core.AspNetCore;
  8. /// <summary>
  9. /// 依赖注入ServiceCollection容器扩展方法
  10. /// </summary>
  11. public static class ServiceCollectionExtensions
  12. {
  13. /// <summary>
  14. /// 注入断点续传服务
  15. /// </summary>
  16. /// <param name="services"></param>
  17. /// <returns></returns>
  18. public static IServiceCollection AddResumeFileResult(this IServiceCollection services)
  19. {
  20. services.TryAddSingleton<IActionResultExecutor<ResumePhysicalFileResult>, ResumePhysicalFileResultExecutor>();
  21. services.TryAddSingleton<IActionResultExecutor<ResumeVirtualFileResult>, ResumeVirtualFileResultExecutor>();
  22. services.TryAddSingleton<IActionResultExecutor<ResumeFileStreamResult>, ResumeFileStreamResultExecutor>();
  23. services.TryAddSingleton<IActionResultExecutor<ResumeFileContentResult>, ResumeFileContentResultExecutor>();
  24. return services;
  25. }
  26. /// <summary>
  27. /// 注入7z压缩
  28. /// </summary>
  29. /// <param name="services"></param>
  30. /// <returns></returns>
  31. public static IServiceCollection AddSevenZipCompressor(this IServiceCollection services)
  32. {
  33. services.AddHttpClient<ISevenZipCompressor, SevenZipCompressor>();
  34. return services;
  35. }
  36. /// <summary>
  37. /// 自动扫描需要注册的服务,被ServiceInject标记的class可自动注入
  38. /// </summary>
  39. /// <param name="services"></param>
  40. public static void AutoRegisterServices(this IServiceCollection services)
  41. {
  42. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  43. services.RegisterServiceByAttribute(assemblies);
  44. services.RegisterBackgroundService(assemblies);
  45. }
  46. /// <summary>
  47. /// 自动扫描需要注册的服务,被ServiceInject标记的class可自动注入
  48. /// </summary>
  49. /// <param name="services"></param>
  50. /// <param name="assemblies"></param>
  51. public static void AutoRegisterServices(this IServiceCollection services, params Assembly[] assemblies)
  52. {
  53. services.RegisterServiceByAttribute(assemblies);
  54. services.RegisterBackgroundService(assemblies);
  55. }
  56. /// <summary>
  57. /// 通过 ServiceAttribute 批量注册服务
  58. /// </summary>
  59. /// <param name="services"></param>
  60. private static void RegisterServiceByAttribute(this IServiceCollection services, IEnumerable<Assembly> assemblies)
  61. {
  62. var types = assemblies.SelectMany(t => t.GetTypes()).Where(t => t.GetCustomAttributes(typeof(ServiceInjectAttribute), false).Length > 0 && t.IsClass && !t.IsAbstract).ToList();
  63. foreach (var type in types)
  64. {
  65. var typeInterface = type.GetInterfaces().FirstOrDefault();
  66. if (typeInterface == null)
  67. {
  68. //服务非继承自接口的直接注入
  69. switch (type.GetCustomAttribute<ServiceInjectAttribute>().Lifetime)
  70. {
  71. case ServiceLifetime.Singleton:
  72. services.AddSingleton(type);
  73. break;
  74. case ServiceLifetime.Scoped:
  75. services.AddScoped(type);
  76. break;
  77. case ServiceLifetime.Transient:
  78. services.AddTransient(type);
  79. break;
  80. }
  81. }
  82. else
  83. {
  84. //服务继承自接口的和接口一起注入
  85. switch (type.GetCustomAttribute<ServiceInjectAttribute>().Lifetime)
  86. {
  87. case ServiceLifetime.Singleton:
  88. services.AddSingleton(type);
  89. services.AddSingleton(typeInterface, type);
  90. break;
  91. case ServiceLifetime.Scoped:
  92. services.AddScoped(type);
  93. services.AddScoped(typeInterface, type);
  94. break;
  95. case ServiceLifetime.Transient:
  96. services.AddTransient(type);
  97. services.AddTransient(typeInterface, type);
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// 注册后台服务
  105. /// </summary>
  106. /// <param name="services"></param>
  107. private static void RegisterBackgroundService(this IServiceCollection services, IEnumerable<Assembly> assemblies)
  108. {
  109. var types = assemblies.SelectMany(t => t.GetTypes()).Where(t => typeof(BackgroundService).IsAssignableFrom(t) && !t.IsAbstract);
  110. foreach (var type in types)
  111. {
  112. services.AddSingleton(typeof(IHostedService), type);
  113. }
  114. }
  115. }
  116. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
  117. public class ServiceInjectAttribute : Attribute
  118. {
  119. public ServiceInjectAttribute()
  120. {
  121. }
  122. public ServiceInjectAttribute(ServiceLifetime lifetime)
  123. {
  124. Lifetime = lifetime;
  125. }
  126. public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Transient;
  127. }