AssemblyInjectSyncStartStopWorker.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Bmp.Core.Common.AssemblyInject.Interfaces;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Hosting;
  4. using Microsoft.Extensions.Logging;
  5. namespace Bmp.Core.Common.AssemblyInject.Services;
  6. internal class AssemblyInjectSyncStartStopWorker<T> : IHostedService
  7. {
  8. private readonly ILogger<AssemblyInjectSyncStartStopWorker<T>> _logger;
  9. private readonly IReadOnlyCollection<IAssemblyInjectSyncInitStarStop> _services;
  10. public AssemblyInjectSyncStartStopWorker(
  11. IServiceProvider serviceProvider,
  12. ILogger<AssemblyInjectSyncStartStopWorker<T>> logger)
  13. {
  14. _logger = logger;
  15. _services = typeof(T).Assembly.GetTypes()
  16. .Where(typeof(IAssemblyInjectSyncInitStarStop).IsAssignableFrom)
  17. .Where(p => p.IsInterface == false && p.IsAbstract == false)
  18. .OrderBy(p => p.Name)
  19. .Select(serviceProvider.GetRequiredService)
  20. .Cast<IAssemblyInjectSyncInitStarStop>()
  21. .ToArray();
  22. _logger.LogInformation("Activating Singleton Component...");
  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($"Initialization...");
  60. if (_services.Count == 0)
  61. {
  62. _logger.LogInformation($"No component found.");
  63. }
  64. else
  65. {
  66. foreach (var service in _services)
  67. {
  68. _logger.LogInformation($"Initializing {service.GetType()}");
  69. service.Init();
  70. }
  71. _logger.LogInformation($"All components initialized.");
  72. }
  73. _logger.LogInformation($"Starting...");
  74. if (_services.Count == 0)
  75. {
  76. _logger.LogInformation($"No component found.");
  77. }
  78. else
  79. {
  80. foreach (var service in _services)
  81. {
  82. _logger.LogInformation($"Starting {service.GetType()}");
  83. service.Start();
  84. }
  85. _logger.LogInformation($"All components started.");
  86. }
  87. }
  88. public async Task StopAsync(CancellationToken cancellationToken)
  89. {
  90. _logger.LogInformation($"Stopping...");
  91. if (_services.Count == 0)
  92. {
  93. _logger.LogInformation($"No component stopped.");
  94. }
  95. else
  96. {
  97. foreach (var service in _services)
  98. {
  99. _logger.LogInformation($"Stopping {service.GetType()}");
  100. service.Stop();
  101. }
  102. _logger.LogInformation($"All components stopped.");
  103. }
  104. }
  105. }