| | | 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 via Needlr. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// Two approaches are supported: |
| | | 16 | | /// </para> |
| | | 17 | | /// <list type="bullet"> |
| | | 18 | | /// <item><description> |
| | | 19 | | /// <b>Reflection-based</b> (<see cref="UseSignalRHubsWithReflection"/>): discovers and maps hubs at |
| | | 20 | | /// runtime. Convenient but incompatible with AOT/trimmed builds. |
| | | 21 | | /// </description></item> |
| | | 22 | | /// <item><description> |
| | | 23 | | /// <b>Source-generated</b> (<c>app.MapGeneratedHubs()</c>): emits hub mappings at compile time. |
| | | 24 | | /// Required for AOT/trimmed applications. |
| | | 25 | | /// </description></item> |
| | | 26 | | /// </list> |
| | | 27 | | /// </remarks> |
| | | 28 | | /// <example> |
| | | 29 | | /// <code> |
| | | 30 | | /// // Reflection-based (not AOT-safe) |
| | | 31 | | /// app.UseSignalRHubsWithReflection(); |
| | | 32 | | /// |
| | | 33 | | /// // Source-generated (AOT-safe) — requires NexusLabs.Needlr.SignalR.Generators |
| | | 34 | | /// app.MapGeneratedHubs(); |
| | | 35 | | /// </code> |
| | | 36 | | /// </example> |
| | | 37 | | public static class SignalRExtensions |
| | | 38 | | { |
| | | 39 | | /// <summary> |
| | | 40 | | /// Registers SignalR hubs using reflection-based discovery. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <remarks> |
| | | 43 | | /// <para> |
| | | 44 | | /// This method uses reflection to discover <see cref="IHubRegistrationPlugin"/> implementations |
| | | 45 | | /// and invoke <c>MapHub<T>()</c> at runtime. |
| | | 46 | | /// </para> |
| | | 47 | | /// <para> |
| | | 48 | | /// <strong>For AOT/trimmed applications</strong>, use the source-generated approach instead: |
| | | 49 | | /// <code> |
| | | 50 | | /// app.MapGeneratedHubs(); // Generated at compile-time, no reflection |
| | | 51 | | /// </code> |
| | | 52 | | /// </para> |
| | | 53 | | /// </remarks> |
| | | 54 | | /// <param name="app">The web application to configure.</param> |
| | | 55 | | /// <param name="pluginFactory">The plugin factory to use for discovering hub registration plugins.</param> |
| | | 56 | | /// <param name="assemblies">The assemblies to scan for hub registration plugins.</param> |
| | | 57 | | /// <returns>The web application for chaining.</returns> |
| | | 58 | | [RequiresUnreferencedCode("SignalR hub registration uses reflection to invoke MapHub<T>(). For AOT scenarios, use ap |
| | | 59 | | [RequiresDynamicCode("SignalR hub registration uses MakeGenericMethod() which requires dynamic code generation.")] |
| | | 60 | | public static WebApplication UseSignalRHubsWithReflection( |
| | | 61 | | this WebApplication app, |
| | | 62 | | IPluginFactory? pluginFactory = null, |
| | | 63 | | IEnumerable<System.Reflection.Assembly>? assemblies = null) |
| | | 64 | | { |
| | 4 | 65 | | ArgumentNullException.ThrowIfNull(app); |
| | | 66 | | |
| | 3 | 67 | | pluginFactory ??= app.Services.GetRequiredService<IPluginFactory>(); |
| | 3 | 68 | | assemblies ??= app.Services.GetRequiredService<IReadOnlyList<System.Reflection.Assembly>>(); |
| | | 69 | | |
| | 3 | 70 | | var plugin = new SignalRHubRegistrationPlugin(); |
| | 3 | 71 | | var assemblyList = assemblies as IReadOnlyList<System.Reflection.Assembly> ?? assemblies.ToList(); |
| | 3 | 72 | | plugin.Configure(new WebApplicationPluginOptions(app, assemblyList, pluginFactory)); |
| | | 73 | | |
| | 3 | 74 | | return app; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Adds the reflection-based SignalR hub registration plugin to the service collection. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <remarks> |
| | | 81 | | /// <para> |
| | | 82 | | /// This registers the <see cref="SignalRHubRegistrationPlugin"/> which uses reflection |
| | | 83 | | /// to discover and map SignalR hubs at runtime. |
| | | 84 | | /// </para> |
| | | 85 | | /// <para> |
| | | 86 | | /// <strong>For AOT/trimmed applications</strong>, do not use this method. Instead, |
| | | 87 | | /// call <c>app.MapGeneratedHubs()</c> directly after building the application. |
| | | 88 | | /// </para> |
| | | 89 | | /// </remarks> |
| | | 90 | | /// <param name="services">The service collection to configure.</param> |
| | | 91 | | /// <returns>The service collection for chaining.</returns> |
| | | 92 | | [RequiresUnreferencedCode("SignalR hub registration uses reflection. For AOT scenarios, use app.MapGeneratedHubs() i |
| | | 93 | | [RequiresDynamicCode("SignalR hub registration uses MakeGenericMethod() which requires dynamic code generation.")] |
| | | 94 | | public static IServiceCollection AddSignalRHubRegistrationWithReflection(this IServiceCollection services) |
| | | 95 | | { |
| | 3 | 96 | | ArgumentNullException.ThrowIfNull(services); |
| | | 97 | | |
| | | 98 | | // Register the plugin so it gets picked up during web application configuration |
| | 2 | 99 | | services.AddSingleton<IWebApplicationPlugin, SignalRHubRegistrationPlugin>(); |
| | 2 | 100 | | return services; |
| | | 101 | | } |
| | | 102 | | } |