From 107e1862b7a463ad2ae413318288ad8461c1a7e8 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Tue, 12 Mar 2019 11:33:55 -0700 Subject: [PATCH] Add HubMetdata (#8425) - Added HubMetadata to detect the hub type on Endpoints - Added a test --- ...t.AspNetCore.SignalR.Core.netcoreapp3.0.cs | 5 ++++ src/SignalR/server/Core/src/HubMetadata.cs | 23 +++++++++++++++++++ .../src/HubEndpointRouteBuilderExtensions.cs | 3 +++ .../server/SignalR/test/MapSignalRTests.cs | 21 +++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 src/SignalR/server/Core/src/HubMetadata.cs diff --git a/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp3.0.cs b/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp3.0.cs index 7e8e10911c..f73ec16dce 100644 --- a/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp3.0.cs +++ b/src/SignalR/server/Core/ref/Microsoft.AspNetCore.SignalR.Core.netcoreapp3.0.cs @@ -183,6 +183,11 @@ namespace Microsoft.AspNetCore.SignalR public abstract System.Threading.Tasks.Task SendUserAsync(string userId, string methodName, object[] args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); public abstract System.Threading.Tasks.Task SendUsersAsync(System.Collections.Generic.IReadOnlyList userIds, string methodName, object[] args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); } + public partial class HubMetadata + { + public HubMetadata(System.Type hubType) { } + public System.Type HubType { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + } [System.AttributeUsageAttribute(System.AttributeTargets.Method, AllowMultiple=false, Inherited=true)] public partial class HubMethodNameAttribute : System.Attribute { diff --git a/src/SignalR/server/Core/src/HubMetadata.cs b/src/SignalR/server/Core/src/HubMetadata.cs new file mode 100644 index 0000000000..65c6fa9e21 --- /dev/null +++ b/src/SignalR/server/Core/src/HubMetadata.cs @@ -0,0 +1,23 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNetCore.SignalR +{ + /// + /// Metadata that describes the information associated with a specific endpoint. + /// + public class HubMetadata + { + public HubMetadata(Type hubType) + { + HubType = hubType; + } + + /// + /// The type of . + /// + public Type HubType { get; } + } +} diff --git a/src/SignalR/server/SignalR/src/HubEndpointRouteBuilderExtensions.cs b/src/SignalR/server/SignalR/src/HubEndpointRouteBuilderExtensions.cs index bcf6b88541..7f4a1be408 100644 --- a/src/SignalR/server/SignalR/src/HubEndpointRouteBuilderExtensions.cs +++ b/src/SignalR/server/SignalR/src/HubEndpointRouteBuilderExtensions.cs @@ -67,6 +67,9 @@ namespace Microsoft.AspNetCore.Builder { e.Metadata.Add(item); } + + // Add metadata that captures the hub type this endpoint is associated with + e.Metadata.Add(new HubMetadata(typeof(THub))); }); return conventionBuilder; diff --git a/src/SignalR/server/SignalR/test/MapSignalRTests.cs b/src/SignalR/server/SignalR/test/MapSignalRTests.cs index ab6aa928ef..a15293d545 100644 --- a/src/SignalR/server/SignalR/test/MapSignalRTests.cs +++ b/src/SignalR/server/SignalR/test/MapSignalRTests.cs @@ -185,6 +185,27 @@ namespace Microsoft.AspNetCore.SignalR.Tests } } + [Fact] + public void MapHubEndPointRoutingAppliesHubMetadata() + { + void ConfigureRoutes(IEndpointRouteBuilder routes) + { + // This "Foo" policy should override the default auth attribute + routes.MapHub("/path"); + } + + using (var host = BuildWebHostWithEndPointRouting(ConfigureRoutes)) + { + host.Start(); + + var dataSource = host.Services.GetRequiredService(); + // We register 2 endpoints (/negotiate and /) + Assert.Equal(2, dataSource.Endpoints.Count); + Assert.Equal(typeof(AuthHub), dataSource.Endpoints[0].Metadata.GetMetadata()?.HubType); + Assert.Equal(typeof(AuthHub), dataSource.Endpoints[1].Metadata.GetMetadata()?.HubType); + } + } + private class InvalidHub : Hub { public void OverloadedMethod(int num)