| | | 1 | | using Microsoft.AspNetCore.Builder; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.AspNet; |
| | | 5 | | |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.SignalR; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Extension methods for configuring SignalR hub registration. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class SignalRExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Registers SignalR hubs using reflection-based discovery. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// <para> |
| | | 20 | | /// This method uses reflection to discover <see cref="IHubRegistrationPlugin"/> implementations |
| | | 21 | | /// and invoke <c>MapHub<T>()</c> at runtime. |
| | | 22 | | /// </para> |
| | | 23 | | /// <para> |
| | | 24 | | /// <strong>For AOT/trimmed applications</strong>, use the source-generated approach instead: |
| | | 25 | | /// <code> |
| | | 26 | | /// app.MapGeneratedHubs(); // Generated at compile-time, no reflection |
| | | 27 | | /// </code> |
| | | 28 | | /// </para> |
| | | 29 | | /// </remarks> |
| | | 30 | | /// <param name="app">The web application to configure.</param> |
| | | 31 | | /// <param name="pluginFactory">The plugin factory to use for discovering hub registration plugins.</param> |
| | | 32 | | /// <param name="assemblies">The assemblies to scan for hub registration plugins.</param> |
| | | 33 | | /// <returns>The web application for chaining.</returns> |
| | | 34 | | [RequiresUnreferencedCode("SignalR hub registration uses reflection to invoke MapHub<T>(). For AOT scenarios, use ap |
| | | 35 | | [RequiresDynamicCode("SignalR hub registration uses MakeGenericMethod() which requires dynamic code generation.")] |
| | | 36 | | public static WebApplication UseSignalRHubsWithReflection( |
| | | 37 | | this WebApplication app, |
| | | 38 | | IPluginFactory? pluginFactory = null, |
| | | 39 | | IEnumerable<System.Reflection.Assembly>? assemblies = null) |
| | | 40 | | { |
| | 4 | 41 | | ArgumentNullException.ThrowIfNull(app); |
| | | 42 | | |
| | 3 | 43 | | pluginFactory ??= app.Services.GetRequiredService<IPluginFactory>(); |
| | 3 | 44 | | assemblies ??= app.Services.GetRequiredService<IReadOnlyList<System.Reflection.Assembly>>(); |
| | | 45 | | |
| | 3 | 46 | | var plugin = new SignalRHubRegistrationPlugin(); |
| | 3 | 47 | | var assemblyList = assemblies as IReadOnlyList<System.Reflection.Assembly> ?? assemblies.ToList(); |
| | 3 | 48 | | plugin.Configure(new WebApplicationPluginOptions(app, assemblyList, pluginFactory)); |
| | | 49 | | |
| | 3 | 50 | | return app; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Adds the reflection-based SignalR hub registration plugin to the service collection. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <remarks> |
| | | 57 | | /// <para> |
| | | 58 | | /// This registers the <see cref="SignalRHubRegistrationPlugin"/> which uses reflection |
| | | 59 | | /// to discover and map SignalR hubs at runtime. |
| | | 60 | | /// </para> |
| | | 61 | | /// <para> |
| | | 62 | | /// <strong>For AOT/trimmed applications</strong>, do not use this method. Instead, |
| | | 63 | | /// call <c>app.MapGeneratedHubs()</c> directly after building the application. |
| | | 64 | | /// </para> |
| | | 65 | | /// </remarks> |
| | | 66 | | /// <param name="services">The service collection to configure.</param> |
| | | 67 | | /// <returns>The service collection for chaining.</returns> |
| | | 68 | | [RequiresUnreferencedCode("SignalR hub registration uses reflection. For AOT scenarios, use app.MapGeneratedHubs() i |
| | | 69 | | [RequiresDynamicCode("SignalR hub registration uses MakeGenericMethod() which requires dynamic code generation.")] |
| | | 70 | | public static IServiceCollection AddSignalRHubRegistrationWithReflection(this IServiceCollection services) |
| | | 71 | | { |
| | 3 | 72 | | ArgumentNullException.ThrowIfNull(services); |
| | | 73 | | |
| | | 74 | | // Register the plugin so it gets picked up during web application configuration |
| | 2 | 75 | | services.AddSingleton<IWebApplicationPlugin, SignalRHubRegistrationPlugin>(); |
| | 2 | 76 | | return services; |
| | | 77 | | } |
| | | 78 | | } |