ServiceCollectionExtensions.cs 5.3 KB

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