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.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<IrcGatewayService> _logger;
private readonly ConcurrentDictionary<string, IrcClientConnection> _connections = new();
@@ -22,11 +23,11 @@ public sealed class IrcGatewayService : BackgroundService
public IrcGatewayService(
IOptions<IrcOptions> options,
IChatService chatService,
IServiceProvider services,
ILogger<IrcGatewayService> 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<IChatService>();
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 _);
@@ -15,8 +15,7 @@ public static class IrcServiceExtensions
if (builder.Configuration.GetValue<bool>("Irc:Enabled"))
{
builder.Services.AddSingleton<IrcGatewayService>();
builder.Services.AddSingleton<IChatBroadcaster>(sp =>
new IrcBroadcaster(sp.GetRequiredService<IrcGatewayService>()));
builder.Services.AddSingleton<IChatBroadcaster, IrcBroadcaster>();
builder.Services.AddHostedService(sp =>
sp.GetRequiredService<IrcGatewayService>());
}
+4
View File
@@ -202,6 +202,10 @@ while (true)
}
}, 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 ────────
Console.Error.WriteLine("[DIAG] Resolving PresenceTracker...");
_ = app.Services.GetRequiredService<PresenceTracker>();