using AyCode.Core.Loggers; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Extensions.Logging; namespace AyCode.Services.SignalRs; /// /// Extension methods for applying to /// a client , plus the /// bundle for consumer-friendly one-line setup (connection + logging bridge). /// public static class AcSignalRConnectionExtensions { /// /// Framework-default builder setup — combines with a /// Microsoft.Extensions.Logging bridge to the supplied . /// Consumers typically call this once when constructing the . /// /// Equivalent to: /// /// builder.AddAcConnection(options) /// .ConfigureLogging(l => { l.SetMinimumLevel(Information); l.AddAcLogger(_ => logger); }); /// /// /// public static IHubConnectionBuilder AddAcDefaults(this IHubConnectionBuilder builder, AcLoggerBase logger, AcHubConnectionOptions connectionOptions) { return builder .AddAcConnection(connectionOptions) .ConfigureLogging(logging => { logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information); logging.AddAcLogger(_ => logger); }); } /// /// Applies to the builder: /// WithUrl (with HttpConnectionOptions), keep-alive, server timeout, /// WithAutomaticReconnect, WithStatefulReconnect. /// /// Nullable properties are skipped when null — the underlying SignalR default is kept. /// Combine freely with any other builder extensions before or after this call; the ordering /// follows the fluent chain (last write wins for a given setting). /// /// public static IHubConnectionBuilder AddAcConnection(this IHubConnectionBuilder builder, AcHubConnectionOptions options) { if (options is null) throw new ArgumentNullException(nameof(options)); if (string.IsNullOrWhiteSpace(options.Url)) throw new ArgumentException("AcHubConnectionOptions.Url must be set.", nameof(options)); builder.WithUrl(options.Url, http => { if (options.Transports.HasValue) http.Transports = options.Transports.Value; if (options.TransportMaxBufferSize.HasValue) http.TransportMaxBufferSize = options.TransportMaxBufferSize.Value; if (options.ApplicationMaxBufferSize.HasValue) http.ApplicationMaxBufferSize = options.ApplicationMaxBufferSize.Value; if (options.CloseTimeout.HasValue) http.CloseTimeout = options.CloseTimeout.Value; if (options.SkipNegotiation.HasValue) http.SkipNegotiation = options.SkipNegotiation.Value; }); if (options.KeepAliveInterval.HasValue) builder.WithKeepAliveInterval(options.KeepAliveInterval.Value); if (options.ServerTimeout.HasValue) builder.WithServerTimeout(options.ServerTimeout.Value); if (options.UseAutomaticReconnect) builder.WithAutomaticReconnect(); if (options.UseStatefulReconnect) builder.WithStatefulReconnect(); return builder; } }