refactor: Update IrcGatewayService to use IServiceProvider for dependency resolution

feat: Simplify IChatBroadcaster registration in IrcServiceExtensions
feat: Add diagnostic logging for IRC configuration and environment
This commit is contained in:
HueByte
2026-02-19 14:09:56 +01:00
parent 13297fd017
commit 8dfdc163bf
3 changed files with 17 additions and 8 deletions
+12 -6
View File
@@ -4,6 +4,7 @@ using System.Net.Security;
using System.Net.Sockets; using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using EchoHub.Core.Contracts; using EchoHub.Core.Contracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@@ -13,7 +14,7 @@ namespace EchoHub.Server.Irc;
public sealed class IrcGatewayService : BackgroundService public sealed class IrcGatewayService : BackgroundService
{ {
private readonly IrcOptions _options; private readonly IrcOptions _options;
private readonly IChatService _chatService; private readonly IServiceProvider _services;
private readonly ILogger<IrcGatewayService> _logger; private readonly ILogger<IrcGatewayService> _logger;
private readonly ConcurrentDictionary<string, IrcClientConnection> _connections = new(); private readonly ConcurrentDictionary<string, IrcClientConnection> _connections = new();
@@ -22,11 +23,11 @@ public sealed class IrcGatewayService : BackgroundService
public IrcGatewayService( public IrcGatewayService(
IOptions<IrcOptions> options, IOptions<IrcOptions> options,
IChatService chatService, IServiceProvider services,
ILogger<IrcGatewayService> logger) ILogger<IrcGatewayService> logger)
{ {
_options = options.Value; _options = options.Value;
_chatService = chatService; _services = services;
_logger = logger; _logger = logger;
} }
@@ -111,10 +112,13 @@ public sealed class IrcGatewayService : BackgroundService
_logger.LogInformation("IRC client connected: {Id}", connection.ConnectionId); _logger.LogInformation("IRC client connected: {Id}", connection.ConnectionId);
IChatService? chatService = null;
try try
{ {
chatService = _services.GetRequiredService<IChatService>();
var handler = new IrcCommandHandler( var handler = new IrcCommandHandler(
connection, _options, _chatService, _logger); connection, _options, chatService, _logger);
await handler.RunAsync(ct); await handler.RunAsync(ct);
} }
@@ -128,10 +132,12 @@ public sealed class IrcGatewayService : BackgroundService
{ {
foreach (var ch in connection.JoinedChannels.ToList()) foreach (var ch in connection.JoinedChannels.ToList())
{ {
await _chatService.LeaveChannelAsync( if (chatService is null) break;
await chatService.LeaveChannelAsync(
connection.ConnectionId, connection.Nickname!, ch); 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 _); _connections.TryRemove(connection.ConnectionId, out _);
@@ -15,8 +15,7 @@ public static class IrcServiceExtensions
if (builder.Configuration.GetValue<bool>("Irc:Enabled")) if (builder.Configuration.GetValue<bool>("Irc:Enabled"))
{ {
builder.Services.AddSingleton<IrcGatewayService>(); builder.Services.AddSingleton<IrcGatewayService>();
builder.Services.AddSingleton<IChatBroadcaster>(sp => builder.Services.AddSingleton<IChatBroadcaster, IrcBroadcaster>();
new IrcBroadcaster(sp.GetRequiredService<IrcGatewayService>()));
builder.Services.AddHostedService(sp => builder.Services.AddHostedService(sp =>
sp.GetRequiredService<IrcGatewayService>()); sp.GetRequiredService<IrcGatewayService>());
} }
+4
View File
@@ -202,6 +202,10 @@ while (true)
} }
}, heartbeatCts.Token); }, heartbeatCts.Token);
// ── Config diagnostics ─────────────────────────────────────────────
Console.Error.WriteLine($"[DIAG] Irc:Enabled = {app.Configuration.GetValue<bool>("Irc:Enabled")}");
Console.Error.WriteLine($"[DIAG] Environment = {app.Environment.EnvironmentName}");
// ── Resolve singletons one-by-one to find which one hangs ──────── // ── Resolve singletons one-by-one to find which one hangs ────────
Console.Error.WriteLine("[DIAG] Resolving PresenceTracker..."); Console.Error.WriteLine("[DIAG] Resolving PresenceTracker...");
_ = app.Services.GetRequiredService<PresenceTracker>(); _ = app.Services.GetRequiredService<PresenceTracker>();