< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Hosting.KestrunHostSignalRExtensions
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Hosting/KestrunHostSignalRExtensions.cs
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 55
Coverable lines: 55
Total lines: 141
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 30
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/15/2025 - 21:27:26 Line coverage: 0% (0/55) Branch coverage: 0% (0/30) Total lines: 141 Tag: Kestrun/Kestrun@c33ec02a85e4f8d6061aeaab5a5e8c3a8b665594 10/15/2025 - 21:27:26 Line coverage: 0% (0/55) Branch coverage: 0% (0/30) Total lines: 141 Tag: Kestrun/Kestrun@c33ec02a85e4f8d6061aeaab5a5e8c3a8b665594

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetConnectedClientCount(...)0%4260%
BroadcastLogAsync()0%7280%
BroadcastEventAsync()0%7280%
BroadcastToGroupAsync()0%7280%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Hosting/KestrunHostSignalRExtensions.cs

#LineLine coverage
 1using Kestrun.SignalR;
 2
 3namespace Kestrun.Hosting;
 4
 5/// <summary>
 6/// Extension methods for KestrunHost to support SignalR real-time broadcasting.
 7/// </summary>
 8public static class KestrunHostSignalRExtensions
 9{
 10    /// <summary>
 11    /// Gets the number of currently connected SignalR clients if the Kestrun hub is configured.
 12    /// </summary>
 13    /// <param name="host">The KestrunHost instance.</param>
 14    /// <returns>The count of connected clients, or null if SignalR hub/connection tracking is not configured.</returns>
 15    public static int? GetConnectedClientCount(this KestrunHost host)
 16    {
 017        var svcProvider = host.App?.Services;
 018        return svcProvider == null
 019            ? null
 020            : svcProvider.GetService(typeof(IConnectionTracker)) is IConnectionTracker tracker ? tracker.ConnectedCount 
 21    }
 22
 23    /// <summary>
 24    /// Broadcasts a log message to all connected SignalR clients using the best available service provider.
 25    /// </summary>
 26    /// <param name="host">The KestrunHost instance.</param>
 27    /// <param name="level">The log level (e.g., Information, Warning, Error, Debug, Verbose).</param>
 28    /// <param name="message">The log message to broadcast.</param>
 29    /// <param name="httpContext">Optional: The current HttpContext, if available.</param>
 30    /// <param name="cancellationToken">Optional: Cancellation token.</param>
 31    public static async Task<bool> BroadcastLogAsync(this KestrunHost host, string level, string message, HttpContext? h
 32    {
 033        if (httpContext != null)
 34        {
 035            var ctx = new Models.KestrunContext(host, httpContext);
 036            return await ctx.BroadcastLogAsync(level, message, cancellationToken).ConfigureAwait(false);
 37        }
 38        // Fallback to service resolution when no HttpContext
 039        var svcProvider = host.App?.Services;
 040        if (svcProvider == null)
 41        {
 042            host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster.");
 043            return false;
 44        }
 045        if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster)
 46        {
 047            host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured with Ke
 048            return false;
 49        }
 50        try
 51        {
 052            await broadcaster.BroadcastLogAsync(level, message, cancellationToken).ConfigureAwait(false);
 053            host.Logger.Debug("Broadcasted log message via SignalR: {Level} - {Message}", level, message);
 054            return true;
 55        }
 056        catch (Exception ex)
 57        {
 058            host.Logger.Error(ex, "Failed to broadcast log message: {Level} - {Message}", level, message);
 059            return false;
 60        }
 061    }
 62
 63    /// <summary>
 64    /// Broadcasts an event to all connected SignalR clients using the best available service provider.
 65    /// </summary>
 66    /// <param name="host">The KestrunHost instance.</param>
 67    /// <param name="eventName">The name of the event to broadcast.</param>
 68    /// <param name="data">Optional data to include with the event.</param>
 69    /// <param name="httpContext">Optional: The current HttpContext, if available.</param>
 70    /// <param name="cancellationToken">Optional: Cancellation token.</param>
 71    public static async Task<bool> BroadcastEventAsync(this KestrunHost host, string eventName, object? data = null, Htt
 72    {
 073        if (httpContext != null)
 74        {
 075            var ctx = new Models.KestrunContext(host, httpContext);
 076            return await ctx.BroadcastEventAsync(eventName, data, cancellationToken).ConfigureAwait(false);
 77        }
 078        var svcProvider = host.App?.Services;
 079        if (svcProvider == null)
 80        {
 081            host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster.");
 082            return false;
 83        }
 084        if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster)
 85        {
 086            host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured with Ke
 087            return false;
 88        }
 89        try
 90        {
 091            await broadcaster.BroadcastEventAsync(eventName, data, cancellationToken).ConfigureAwait(false);
 092            host.Logger.Debug("Broadcasted event via SignalR: {EventName}", eventName);
 093            return true;
 94        }
 095        catch (Exception ex)
 96        {
 097            host.Logger.Error(ex, "Failed to broadcast event: {EventName}", eventName);
 098            return false;
 99        }
 0100    }
 101
 102    /// <summary>
 103    /// Broadcasts a message to a specific SignalR group using the best available service provider.
 104    /// </summary>
 105    /// <param name="host">The KestrunHost instance.</param>
 106    /// <param name="groupName">The name of the group to broadcast to.</param>
 107    /// <param name="method">The hub method name to invoke on clients.</param>
 108    /// <param name="message">The message to broadcast.</param>
 109    /// <param name="httpContext">Optional: The current HttpContext, if available.</param>
 110    /// <param name="cancellationToken">Optional: Cancellation token.</param>
 111    public static async Task<bool> BroadcastToGroupAsync(this KestrunHost host, string groupName, string method, object?
 112    {
 0113        if (httpContext != null)
 114        {
 0115            var ctx = new Models.KestrunContext(host, httpContext);
 0116            return await ctx.BroadcastToGroupAsync(groupName, method, message, cancellationToken).ConfigureAwait(false);
 117        }
 0118        var svcProvider = host.App?.Services;
 0119        if (svcProvider == null)
 120        {
 0121            host.Logger.Warning("No service provider available to resolve IRealtimeBroadcaster.");
 0122            return false;
 123        }
 0124        if (svcProvider.GetService(typeof(IRealtimeBroadcaster)) is not IRealtimeBroadcaster broadcaster)
 125        {
 0126            host.Logger.Warning("IRealtimeBroadcaster service is not registered. Make sure SignalR is configured with Ke
 0127            return false;
 128        }
 129        try
 130        {
 0131            await broadcaster.BroadcastToGroupAsync(groupName, method, message, cancellationToken).ConfigureAwait(false)
 0132            host.Logger.Debug("Broadcasted to group {GroupName} via method {Method}", groupName, method);
 0133            return true;
 134        }
 0135        catch (Exception ex)
 136        {
 0137            host.Logger.Error(ex, "Failed to broadcast to group {GroupName}", groupName);
 0138            return false;
 139        }
 0140    }
 141}