ServiceCollectionExtensions.cs 4.3 KB

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