| | | 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 | | /// Hub endpoints are never mapped automatically. Call exactly one mapping extension |
| | | 28 | | /// after building the application. |
| | | 29 | | /// </remarks> |
| | | 30 | | /// <example> |
| | | 31 | | /// <code> |
| | | 32 | | /// // Reflection-based (not AOT-safe) |
| | | 33 | | /// app.UseSignalRHubsWithReflection(); |
| | | 34 | | /// |
| | | 35 | | /// // Source-generated (AOT-safe) — requires NexusLabs.Needlr.SignalR.Generators |
| | | 36 | | /// app.MapGeneratedHubs(); |
| | | 37 | | /// </code> |
| | | 38 | | /// </example> |
| | | 39 | | public static class SignalRExtensions |
| | | 40 | | { |
| | | 41 | | /// <summary> |
| | | 42 | | /// Registers SignalR hubs using reflection-based discovery. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <remarks> |
| | | 45 | | /// <para> |
| | | 46 | | /// This method uses reflection to discover <see cref="IHubRegistrationPlugin"/> implementations |
| | | 47 | | /// and invoke <c>MapHub<T>()</c> at runtime. |
| | | 48 | | /// </para> |
| | | 49 | | /// <para> |
| | | 50 | | /// This is an explicit post-build operation. Needlr does not add the reflection |
| | | 51 | | /// mapper to automatic web-application plugin discovery. |
| | | 52 | | /// </para> |
| | | 53 | | /// <para> |
| | | 54 | | /// <strong>For AOT/trimmed applications</strong>, use the source-generated approach instead: |
| | | 55 | | /// <code> |
| | | 56 | | /// app.MapGeneratedHubs(); // Generated at compile-time, no reflection |
| | | 57 | | /// </code> |
| | | 58 | | /// </para> |
| | | 59 | | /// </remarks> |
| | | 60 | | /// <param name="app">The web application to configure.</param> |
| | | 61 | | /// <param name="pluginFactory">The plugin factory to use for discovering hub registration plugins.</param> |
| | | 62 | | /// <param name="assemblies">The assemblies to scan for hub registration plugins.</param> |
| | | 63 | | /// <returns>The web application for chaining.</returns> |
| | | 64 | | [RequiresUnreferencedCode("SignalR hub registration uses reflection to invoke MapHub<T>(). For AOT scenarios, use ap |
| | | 65 | | [RequiresDynamicCode("SignalR hub registration uses MakeGenericMethod() which requires dynamic code generation.")] |
| | | 66 | | public static WebApplication UseSignalRHubsWithReflection( |
| | | 67 | | this WebApplication app, |
| | | 68 | | IPluginFactory? pluginFactory = null, |
| | | 69 | | IEnumerable<System.Reflection.Assembly>? assemblies = null) |
| | | 70 | | { |
| | 5 | 71 | | ArgumentNullException.ThrowIfNull(app); |
| | | 72 | | |
| | 4 | 73 | | pluginFactory ??= app.Services.GetRequiredService<IPluginFactory>(); |
| | 4 | 74 | | assemblies ??= app.Services.GetRequiredService<IReadOnlyList<System.Reflection.Assembly>>(); |
| | | 75 | | |
| | 4 | 76 | | var assemblyList = assemblies as IReadOnlyList<System.Reflection.Assembly> ?? assemblies.ToList(); |
| | 4 | 77 | | new SignalRHubReflectionMapper().Map( |
| | 4 | 78 | | app, |
| | 4 | 79 | | assemblyList, |
| | 4 | 80 | | pluginFactory); |
| | | 81 | | |
| | 4 | 82 | | return app; |
| | | 83 | | } |
| | | 84 | | } |