AssemblyInjectSyncStartStopWorker.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.Hosting;
  3. using Microsoft.Extensions.Logging;
  4. using WarcViewerBlazorWinForm.Library.AssemblyInject.Interfaces;
  5. namespace WarcViewerBlazorWinForm.Library.AssemblyInject.Services
  6. {
  7. internal class AssemblyInjectSyncStartStopWorker<T> : IHostedService
  8. {
  9. private readonly ILogger<AssemblyInjectSyncStartStopWorker<T>> _logger;
  10. private readonly IReadOnlyCollection<IAssemblyInjectSyncStarStop> _services;
  11. public AssemblyInjectSyncStartStopWorker(
  12. IServiceProvider serviceProvider,
  13. ILogger<AssemblyInjectSyncStartStopWorker<T>> logger)
  14. {
  15. _logger = logger;
  16. _services = typeof(T).Assembly.GetTypes()
  17. .Where(typeof(IAssemblyInjectSyncStarStop).IsAssignableFrom)
  18. .Where(p => p.IsInterface == false && p.IsAbstract == false)
  19. .OrderBy(p => p.Name)
  20. .Select(serviceProvider.GetRequiredService)
  21. .Cast<IAssemblyInjectSyncStarStop>()
  22. .ToArray();
  23. //单例组件
  24. var serviceTypes = new HashSet<Type>();
  25. foreach (var svcType in typeof(T).Assembly.GetTypes()
  26. .Where(typeof(IAssemblyInjectSingleton).IsAssignableFrom)
  27. .Where(p => p.IsInterface == false && p.IsAbstract == false)
  28. .OrderBy(p => p.Name))
  29. {
  30. var gsi = svcType.GetInterfaces().Where(p => p.IsGenericType && p.GetGenericTypeDefinition() == typeof(IAssemblyInjectSingleton<>)).ToArray();
  31. foreach (var type in gsi)
  32. {
  33. var addAs = type.GetGenericArguments().First()!;
  34. serviceTypes.Add(addAs);
  35. }
  36. }
  37. foreach (var serviceType in serviceTypes)
  38. {
  39. var services = serviceProvider.GetServices(serviceType);
  40. foreach (var service in services)
  41. {
  42. _logger.LogInformation($"Singleton Component Active {service.GetType().FullName}");
  43. }
  44. }
  45. foreach (var type in typeof(T).Assembly.GetTypes()
  46. .Where(type => typeof(IAssemblyInjectSingleton).IsAssignableFrom(type) && !typeof(IAssemblyInjectSingleton<>).IsAssignableFrom(type))
  47. .Where(p => p.IsInterface == false && p.IsAbstract == false)
  48. .OrderBy(p => p.Name))
  49. {
  50. var service = serviceProvider.GetService(type);
  51. if (service != null)
  52. {
  53. _logger.LogInformation($"Singleton Component Active {service.GetType().FullName}");
  54. }
  55. }
  56. }
  57. public async Task StartAsync(CancellationToken cancellationToken)
  58. {
  59. _logger.LogInformation($"Starting...");
  60. if (_services.Count == 0)
  61. {
  62. _logger.LogInformation($"No component started.");
  63. }
  64. else
  65. {
  66. foreach (var service in _services)
  67. {
  68. _logger.LogInformation($"Starting {service.GetType()}");
  69. service.Start();
  70. }
  71. _logger.LogInformation($"All components started.");
  72. }
  73. }
  74. public async Task StopAsync(CancellationToken cancellationToken)
  75. {
  76. _logger.LogInformation($"Stopping...");
  77. if (_services.Count == 0)
  78. {
  79. _logger.LogInformation($"No component stopped.");
  80. }
  81. else
  82. {
  83. foreach (var service in _services)
  84. {
  85. _logger.LogInformation($"Stopping {service.GetType()}");
  86. service.Stop();
  87. }
  88. _logger.LogInformation($"All components stopped.");
  89. }
  90. }
  91. }
  92. }