ServiceCollectionExtensions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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: services.AddSingleton(type); break;
  72. case ServiceLifetime.Scoped: services.AddScoped(type); break;
  73. case ServiceLifetime.Transient: services.AddTransient(type); break;
  74. }
  75. }
  76. else
  77. {
  78. //服务继承自接口的和接口一起注入
  79. switch (type.GetCustomAttribute<ServiceInjectAttribute>().Lifetime)
  80. {
  81. case ServiceLifetime.Singleton: services.AddSingleton(typeInterface, type); break;
  82. case ServiceLifetime.Scoped: services.AddScoped(typeInterface, type); break;
  83. case ServiceLifetime.Transient: services.AddTransient(typeInterface, type); break;
  84. }
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// 注册后台服务
  90. /// </summary>
  91. /// <param name="services"></param>
  92. private static void RegisterBackgroundService(this IServiceCollection services, IEnumerable<Assembly> assemblies)
  93. {
  94. var types = assemblies.SelectMany(t => t.GetTypes()).Where(t => typeof(BackgroundService).IsAssignableFrom(t) && !t.IsAbstract);
  95. foreach (var type in types)
  96. {
  97. services.AddSingleton(typeof(IHostedService), type);
  98. }
  99. }
  100. }
  101. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
  102. public class ServiceInjectAttribute : Attribute
  103. {
  104. public ServiceInjectAttribute()
  105. {
  106. }
  107. public ServiceInjectAttribute(ServiceLifetime lifetime)
  108. {
  109. Lifetime = lifetime;
  110. }
  111. public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Transient;
  112. }