using System; using System.Threading; using System.Threading.Tasks; using EntrustSettle.Common; using EntrustSettle.EventBus; using EntrustSettle.EventBus.EventHandling; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace EntrustSettle.Extensions.HostedService; public class EventBusHostedService : IHostedService { private readonly IServiceProvider _serviceProvider; private readonly ILogger _logger; public EventBusHostedService(IServiceProvider serviceProvider, ILogger logger) { _serviceProvider = serviceProvider; _logger = logger; } public async Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("Start EventBus Service!"); await DoWork(); } private Task DoWork() { var eventBus = _serviceProvider.GetRequiredService(); eventBus.Subscribe(); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("Stop EventBus Service!"); return Task.CompletedTask; } }