123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using Bmp.Core.Common.AssemblyInject.Interfaces;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- namespace Bmp.Core.Common.AssemblyInject.Services;
- internal class AssemblyInjectSyncStartStopWorker<T> : IHostedService
- {
- private readonly ILogger<AssemblyInjectSyncStartStopWorker<T>> _logger;
- private readonly IReadOnlyCollection<IAssemblyInjectSyncInitStarStop> _services;
- public AssemblyInjectSyncStartStopWorker(
- IServiceProvider serviceProvider,
- ILogger<AssemblyInjectSyncStartStopWorker<T>> logger)
- {
- _logger = logger;
- _services = typeof(T).Assembly.GetTypes()
- .Where(typeof(IAssemblyInjectSyncInitStarStop).IsAssignableFrom)
- .Where(p => p.IsInterface == false && p.IsAbstract == false)
- .OrderBy(p => p.Name)
- .Select(serviceProvider.GetRequiredService)
- .Cast<IAssemblyInjectSyncInitStarStop>()
- .ToArray();
- _logger.LogInformation("Activating Singleton Component...");
- //单例组件
- var serviceTypes = new HashSet<Type>();
- foreach (var svcType in typeof(T).Assembly.GetTypes()
- .Where(typeof(IAssemblyInjectSingleton).IsAssignableFrom)
- .Where(p => p.IsInterface == false && p.IsAbstract == false)
- .OrderBy(p => p.Name))
- {
- var gsi = svcType.GetInterfaces().Where(p => p.IsGenericType && p.GetGenericTypeDefinition() == typeof(IAssemblyInjectSingleton<>)).ToArray();
- foreach (var type in gsi)
- {
- var addAs = type.GetGenericArguments().First()!;
- serviceTypes.Add(addAs);
- }
- }
- foreach (var serviceType in serviceTypes)
- {
- var services = serviceProvider.GetServices(serviceType);
- foreach (var service in services)
- {
- _logger.LogInformation($"Singleton Component Active {service.GetType().FullName}");
- }
- }
- foreach (var type in typeof(T).Assembly.GetTypes()
- .Where(type => typeof(IAssemblyInjectSingleton).IsAssignableFrom(type) && !typeof(IAssemblyInjectSingleton<>).IsAssignableFrom(type))
- .Where(p => p.IsInterface == false && p.IsAbstract == false)
- .OrderBy(p => p.Name))
- {
- var service = serviceProvider.GetService(type);
- if (service != null)
- {
- _logger.LogInformation($"Singleton Component Active {service.GetType().FullName}");
- }
- }
- }
- public async Task StartAsync(CancellationToken cancellationToken)
- {
- _logger.LogInformation($"Initialization...");
- if (_services.Count == 0)
- {
- _logger.LogInformation($"No component found.");
- }
- else
- {
- foreach (var service in _services)
- {
- _logger.LogInformation($"Initializing {service.GetType()}");
- service.Init();
- }
- _logger.LogInformation($"All components initialized.");
- }
- _logger.LogInformation($"Starting...");
- if (_services.Count == 0)
- {
- _logger.LogInformation($"No component found.");
- }
- else
- {
- foreach (var service in _services)
- {
- _logger.LogInformation($"Starting {service.GetType()}");
- service.Start();
- }
- _logger.LogInformation($"All components started.");
- }
- }
- public async Task StopAsync(CancellationToken cancellationToken)
- {
- _logger.LogInformation($"Stopping...");
- if (_services.Count == 0)
- {
- _logger.LogInformation($"No component stopped.");
- }
- else
- {
- foreach (var service in _services)
- {
- _logger.LogInformation($"Stopping {service.GetType()}");
- service.Stop();
- }
- _logger.LogInformation($"All components stopped.");
- }
- }
- }
|