< Summary

Information
Class: NexusLabs.Needlr.SignalR.SignalRHubRegistrationPlugin
Assembly: NexusLabs.Needlr.SignalR
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.SignalR/SignalRHubRegistrationPlugin.cs
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 64
Line coverage: 100%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Configure(...)66.66%66100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.SignalR/SignalRHubRegistrationPlugin.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Builder;
 2using Microsoft.Extensions.DependencyInjection;
 3using Microsoft.Extensions.Logging;
 4
 5using NexusLabs.Needlr.AspNet;
 6
 7using System.Diagnostics.CodeAnalysis;
 8
 9namespace NexusLabs.Needlr.SignalR;
 10
 11/// <summary>
 12/// Plugin that registers SignalR hubs discovered via <see cref="IHubRegistrationPlugin"/> implementations.
 13/// </summary>
 14/// <remarks>
 15/// <para>
 16/// This plugin uses reflection to invoke MapHub&lt;T&gt;() at runtime because the SignalR API
 17/// does not provide a non-generic overload.
 18/// </para>
 19/// <para>
 20/// <strong>For AOT/trimmed applications</strong>, use the source-generated approach instead:
 21/// <code>
 22/// var app = builder.Build();
 23/// app.MapGeneratedHubs(); // Generated at compile-time, no reflection
 24/// </code>
 25/// </para>
 26/// <para>
 27/// This plugin is marked with <see cref="DoNotAutoRegisterAttribute"/> to prevent automatic
 28/// registration in source-gen scenarios. To use this reflection-based approach, explicitly
 29/// register the plugin or call <see cref="SignalRExtensions.UseSignalRHubsWithReflection"/>.
 30/// </para>
 31/// </remarks>
 32[DoNotAutoRegister]
 33[RequiresUnreferencedCode("SignalR hub registration uses reflection to invoke MapHub<T>(). For AOT scenarios, use app.Ma
 34[RequiresDynamicCode("SignalR hub registration uses MakeGenericMethod() which requires dynamic code generation.")]
 35public sealed class SignalRHubRegistrationPlugin : IWebApplicationPlugin
 36{
 37    /// <inheritdoc />
 38    public void Configure(WebApplicationPluginOptions options)
 39    {
 740        ArgumentNullException.ThrowIfNull(options);
 41
 742        var pluginFactory = options.WebApplication.Services.GetRequiredService<IPluginFactory>();
 43
 744        options.WebApplication.Logger.LogInformation("Configuring SignalR hubs...");
 45
 46        // NOTE: this is gross reflection because there is no overload to use
 47        // Type objects instead of the generic type parameters
 748        var mapHubMethod = typeof(HubEndpointRouteBuilderExtensions)
 749            .GetMethods()
 1450            .First(m => m.Name == "MapHub" &&
 1451                m.IsGenericMethodDefinition &&
 1452                m.GetParameters().Length == 2);
 53
 2654        foreach (var plugin in pluginFactory.CreatePluginsFromAssemblies<IHubRegistrationPlugin>(
 755            options.Assemblies))
 56        {
 657            options.WebApplication.Logger.LogInformation("Registering SignalR hub '{HubName}'...", plugin.GetType().Name
 658            var genericMapHub = mapHubMethod.MakeGenericMethod(plugin.HubType);
 659            genericMapHub.Invoke(null, new object[] { options.WebApplication, plugin.HubPath });
 60        }
 61
 762        options.WebApplication.Logger.LogInformation("SignalR hubs configured successfully.");
 763    }
 64}