using System; using System.Threading; using System.Threading.Tasks; using Tiobon.Core.Common; using Tiobon.Core.EventBus; using Tiobon.Core.EventBus.EventHandling; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace Tiobon.Core.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() { if (AppSettings.app(new string[] { "EventBus", "Enabled" }).ObjToBool()) { var eventBus = _serviceProvider.GetRequiredService(); eventBus.Subscribe(); } return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("Stop EventBus Service!"); return Task.CompletedTask; } }