| | | 1 | | using Microsoft.AspNetCore.Builder; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | using NexusLabs.Needlr.AspNet; |
| | | 6 | | |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | |
| | | 9 | | namespace 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<T>() 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.")] |
| | | 35 | | public sealed class SignalRHubRegistrationPlugin : IWebApplicationPlugin |
| | | 36 | | { |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public void Configure(WebApplicationPluginOptions options) |
| | | 39 | | { |
| | 7 | 40 | | ArgumentNullException.ThrowIfNull(options); |
| | | 41 | | |
| | 7 | 42 | | var pluginFactory = options.WebApplication.Services.GetRequiredService<IPluginFactory>(); |
| | | 43 | | |
| | 7 | 44 | | 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 |
| | 7 | 48 | | var mapHubMethod = typeof(HubEndpointRouteBuilderExtensions) |
| | 7 | 49 | | .GetMethods() |
| | 14 | 50 | | .First(m => m.Name == "MapHub" && |
| | 14 | 51 | | m.IsGenericMethodDefinition && |
| | 14 | 52 | | m.GetParameters().Length == 2); |
| | | 53 | | |
| | 26 | 54 | | foreach (var plugin in pluginFactory.CreatePluginsFromAssemblies<IHubRegistrationPlugin>( |
| | 7 | 55 | | options.Assemblies)) |
| | | 56 | | { |
| | 6 | 57 | | options.WebApplication.Logger.LogInformation("Registering SignalR hub '{HubName}'...", plugin.GetType().Name |
| | 6 | 58 | | var genericMapHub = mapHubMethod.MakeGenericMethod(plugin.HubType); |
| | 6 | 59 | | genericMapHub.Invoke(null, new object[] { options.WebApplication, plugin.HubPath }); |
| | | 60 | | } |
| | | 61 | | |
| | 7 | 62 | | options.WebApplication.Logger.LogInformation("SignalR hubs configured successfully."); |
| | 7 | 63 | | } |
| | | 64 | | } |