diff --git a/src/EchoHub.Server.Irc/IrcGatewayService.cs b/src/EchoHub.Server.Irc/IrcGatewayService.cs index 4292ef8..5515b9c 100644 --- a/src/EchoHub.Server.Irc/IrcGatewayService.cs +++ b/src/EchoHub.Server.Irc/IrcGatewayService.cs @@ -4,6 +4,7 @@ using System.Net.Security; using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; using EchoHub.Core.Contracts; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -13,7 +14,7 @@ namespace EchoHub.Server.Irc; public sealed class IrcGatewayService : BackgroundService { private readonly IrcOptions _options; - private readonly IChatService _chatService; + private readonly IServiceProvider _services; private readonly ILogger _logger; private readonly ConcurrentDictionary _connections = new(); @@ -22,11 +23,11 @@ public sealed class IrcGatewayService : BackgroundService public IrcGatewayService( IOptions options, - IChatService chatService, + IServiceProvider services, ILogger logger) { _options = options.Value; - _chatService = chatService; + _services = services; _logger = logger; } @@ -111,10 +112,13 @@ public sealed class IrcGatewayService : BackgroundService _logger.LogInformation("IRC client connected: {Id}", connection.ConnectionId); + IChatService? chatService = null; + try { + chatService = _services.GetRequiredService(); var handler = new IrcCommandHandler( - connection, _options, _chatService, _logger); + connection, _options, chatService, _logger); await handler.RunAsync(ct); } @@ -128,10 +132,12 @@ public sealed class IrcGatewayService : BackgroundService { foreach (var ch in connection.JoinedChannels.ToList()) { - await _chatService.LeaveChannelAsync( + if (chatService is null) break; + await chatService.LeaveChannelAsync( connection.ConnectionId, connection.Nickname!, ch); } - await _chatService.UserDisconnectedAsync(connection.ConnectionId); + if (chatService is not null) + await chatService.UserDisconnectedAsync(connection.ConnectionId); } _connections.TryRemove(connection.ConnectionId, out _); diff --git a/src/EchoHub.Server.Irc/IrcServiceExtensions.cs b/src/EchoHub.Server.Irc/IrcServiceExtensions.cs index 14c096b..7af3647 100644 --- a/src/EchoHub.Server.Irc/IrcServiceExtensions.cs +++ b/src/EchoHub.Server.Irc/IrcServiceExtensions.cs @@ -15,8 +15,7 @@ public static class IrcServiceExtensions if (builder.Configuration.GetValue("Irc:Enabled")) { builder.Services.AddSingleton(); - builder.Services.AddSingleton(sp => - new IrcBroadcaster(sp.GetRequiredService())); + builder.Services.AddSingleton(); builder.Services.AddHostedService(sp => sp.GetRequiredService()); } diff --git a/src/EchoHub.Server/Program.cs b/src/EchoHub.Server/Program.cs index 17c7b85..809355e 100644 --- a/src/EchoHub.Server/Program.cs +++ b/src/EchoHub.Server/Program.cs @@ -202,6 +202,10 @@ while (true) } }, heartbeatCts.Token); + // ── Config diagnostics ───────────────────────────────────────────── + Console.Error.WriteLine($"[DIAG] Irc:Enabled = {app.Configuration.GetValue("Irc:Enabled")}"); + Console.Error.WriteLine($"[DIAG] Environment = {app.Environment.EnvironmentName}"); + // ── Resolve singletons one-by-one to find which one hangs ──────── Console.Error.WriteLine("[DIAG] Resolving PresenceTracker..."); _ = app.Services.GetRequiredService();