| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | using Microsoft.AspNetCore.Builder; |
| | | 5 | | using Microsoft.AspNetCore.SignalR; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | using NexusLabs.Needlr.AspNet; |
| | | 9 | | |
| | | 10 | | namespace NexusLabs.Needlr.SignalR; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Maps explicitly requested SignalR hubs through reflection without participating |
| | | 14 | | /// in Needlr plugin discovery. |
| | | 15 | | /// </summary> |
| | | 16 | | [DoNotAutoRegister] |
| | | 17 | | internal sealed partial class SignalRHubReflectionMapper |
| | | 18 | | { |
| | | 19 | | [RequiresUnreferencedCode("SignalR hub registration uses reflection to invoke MapHub<T>(). For AOT scenarios, use ap |
| | | 20 | | [RequiresDynamicCode("SignalR hub registration uses MakeGenericMethod() which requires dynamic code generation.")] |
| | | 21 | | internal void Map( |
| | | 22 | | WebApplication app, |
| | | 23 | | IReadOnlyList<Assembly> assemblies, |
| | | 24 | | IPluginFactory pluginFactory) |
| | | 25 | | { |
| | 4 | 26 | | ArgumentNullException.ThrowIfNull(app); |
| | 4 | 27 | | ArgumentNullException.ThrowIfNull(assemblies); |
| | 4 | 28 | | ArgumentNullException.ThrowIfNull(pluginFactory); |
| | | 29 | | |
| | 4 | 30 | | LogConfiguringHubs(app.Logger); |
| | | 31 | | |
| | 4 | 32 | | var mapHubMethod = typeof(HubEndpointRouteBuilderExtensions) |
| | 4 | 33 | | .GetMethods() |
| | 4 | 34 | | .First(method => |
| | 8 | 35 | | method.Name == "MapHub" && |
| | 8 | 36 | | method.IsGenericMethodDefinition && |
| | 8 | 37 | | method.GetParameters().Length == 2); |
| | | 38 | | |
| | 16 | 39 | | foreach (var plugin in |
| | 4 | 40 | | pluginFactory.CreatePluginsFromAssemblies<IHubRegistrationPlugin>( |
| | 4 | 41 | | assemblies)) |
| | | 42 | | { |
| | 6 | 43 | | LogRegisteringHub(app.Logger, plugin.GetType().Name); |
| | 6 | 44 | | var genericMapHub = mapHubMethod.MakeGenericMethod(plugin.HubType); |
| | 6 | 45 | | genericMapHub.Invoke( |
| | 6 | 46 | | null, |
| | 6 | 47 | | [app, plugin.HubPath]); |
| | | 48 | | } |
| | | 49 | | |
| | 4 | 50 | | LogHubsConfigured(app.Logger); |
| | 4 | 51 | | } |
| | | 52 | | |
| | | 53 | | [LoggerMessage( |
| | | 54 | | Level = LogLevel.Information, |
| | | 55 | | Message = "Configuring SignalR hubs...")] |
| | | 56 | | private static partial void LogConfiguringHubs(ILogger logger); |
| | | 57 | | |
| | | 58 | | [LoggerMessage( |
| | | 59 | | Level = LogLevel.Information, |
| | | 60 | | Message = "Registering SignalR hub '{HubName}'...")] |
| | | 61 | | private static partial void LogRegisteringHub( |
| | | 62 | | ILogger logger, |
| | | 63 | | string hubName); |
| | | 64 | | |
| | | 65 | | [LoggerMessage( |
| | | 66 | | Level = LogLevel.Information, |
| | | 67 | | Message = "SignalR hubs configured successfully.")] |
| | | 68 | | private static partial void LogHubsConfigured(ILogger logger); |
| | | 69 | | } |