123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using WarcViewerBlazorWinForm.Library.AssemblyInject.Interfaces;
- namespace WarcViewerBlazorWinForm.Library.AssemblyInject.Services
- {
- internal class AssemblyInjectSyncStartStopWorker<T> : IHostedService
- {
- private readonly ILogger<AssemblyInjectSyncStartStopWorker<T>> _logger;
- private readonly IReadOnlyCollection<IAssemblyInjectSyncStarStop> _services;
- public AssemblyInjectSyncStartStopWorker(
- IServiceProvider serviceProvider,
- ILogger<AssemblyInjectSyncStartStopWorker<T>> logger)
- {
- _logger = logger;
- _services = typeof(T).Assembly.GetTypes()
- .Where(typeof(IAssemblyInjectSyncStarStop).IsAssignableFrom)
- .Where(p => p.IsInterface == false && p.IsAbstract == false)
- .OrderBy(p => p.Name)
- .Select(serviceProvider.GetRequiredService)
- .Cast<IAssemblyInjectSyncStarStop>()
- .ToArray();
- //单例组件
- 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($"Starting...");
- if (_services.Count == 0)
- {
- _logger.LogInformation($"No component started.");
- }
- 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.");
- }
- }
- }
- }
|