ServiceCollectionExtensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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;
  6. using Microsoft.Extensions.DependencyInjection.Extensions;
  7. using Microsoft.Extensions.Hosting;
  8. using System.Reflection;
  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. /// 通过 ServiceAttribute 批量注册服务
  50. /// </summary>
  51. /// <param name="services"></param>
  52. private static void RegisterServiceByAttribute(this IServiceCollection services, IEnumerable<Assembly> assemblies)
  53. {
  54. var types = assemblies.SelectMany(t => t.GetTypes()).Where(t => t.GetCustomAttributes(typeof(ServiceInjectAttribute), false).Length > 0 && t.IsClass && !t.IsAbstract).ToList();
  55. foreach (var type in types)
  56. {
  57. var typeInterface = type.GetInterfaces().FirstOrDefault();
  58. if (typeInterface == null)
  59. {
  60. //服务非继承自接口的直接注入
  61. switch (type.GetCustomAttribute<ServiceInjectAttribute>().Lifetime)
  62. {
  63. case ServiceLifetime.Singleton: services.AddSingleton(type); break;
  64. case ServiceLifetime.Scoped: services.AddScoped(type); break;
  65. case ServiceLifetime.Transient: services.AddTransient(type); break;
  66. }
  67. }
  68. else
  69. {
  70. //服务继承自接口的和接口一起注入
  71. switch (type.GetCustomAttribute<ServiceInjectAttribute>().Lifetime)
  72. {
  73. case ServiceLifetime.Singleton: services.AddSingleton(typeInterface, type); break;
  74. case ServiceLifetime.Scoped: services.AddScoped(typeInterface, type); break;
  75. case ServiceLifetime.Transient: services.AddTransient(typeInterface, type); break;
  76. }
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 注册后台服务
  82. /// </summary>
  83. /// <param name="services"></param>
  84. private static void RegisterBackgroundService(this IServiceCollection services, IEnumerable<Assembly> assemblies)
  85. {
  86. var types = assemblies.SelectMany(t => t.GetTypes()).Where(t => typeof(BackgroundService).IsAssignableFrom(t) && !t.IsAbstract);
  87. foreach (var type in types)
  88. {
  89. services.AddSingleton(typeof(IHostedService), type);
  90. }
  91. }
  92. }
  93. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
  94. public class ServiceInjectAttribute : Attribute
  95. {
  96. public ServiceInjectAttribute()
  97. {
  98. }
  99. public ServiceInjectAttribute(ServiceLifetime lifetime)
  100. {
  101. Lifetime = lifetime;
  102. }
  103. public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Transient;
  104. }