From 3005337a9c1dc5d932053ccd773f64f01a14c7da Mon Sep 17 00:00:00 2001 From: Andrew Stanton-Nurse Date: Fri, 1 Dec 2017 09:48:26 -0800 Subject: [PATCH] fix #625 by implementing HubMethodNameAttribute (#1173) --- .../HubEndPoint.cs | 4 +- .../HubMethodNameAttribute.cs | 20 ++++++ .../Microsoft.AspNetCore.SignalR.Core.csproj | 1 + .../HubEndpointTests.cs | 64 +++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNetCore.SignalR.Core/HubMethodNameAttribute.cs diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs index 5a31bcb138..0e9ce1d65c 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs @@ -598,7 +598,9 @@ namespace Microsoft.AspNetCore.SignalR foreach (var methodInfo in HubReflectionHelper.GetHubMethods(hubType)) { - var methodName = methodInfo.Name; + var methodName = + methodInfo.GetCustomAttribute()?.Name ?? + methodInfo.Name; if (_methods.ContainsKey(methodName)) { diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubMethodNameAttribute.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubMethodNameAttribute.cs new file mode 100644 index 0000000000..e73059598d --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubMethodNameAttribute.cs @@ -0,0 +1,20 @@ +// 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; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.AspNetCore.SignalR +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public class HubMethodNameAttribute : Attribute + { + public string Name { get; } + + public HubMethodNameAttribute(string name) + { + Name = name; + } + } +} diff --git a/src/Microsoft.AspNetCore.SignalR.Core/Microsoft.AspNetCore.SignalR.Core.csproj b/src/Microsoft.AspNetCore.SignalR.Core/Microsoft.AspNetCore.SignalR.Core.csproj index b1d6924373..77bb7acd30 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/Microsoft.AspNetCore.SignalR.Core.csproj +++ b/src/Microsoft.AspNetCore.SignalR.Core/Microsoft.AspNetCore.SignalR.Core.csproj @@ -3,6 +3,7 @@ Real-time communication framework for ASP.NET Core. netstandard2.0 + Microsoft.AspNetCore.SignalR diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs index 6c05efc117..fd237c0ec9 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs @@ -11,6 +11,7 @@ using System.Threading.Channels; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.SignalR.Core; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.SignalR.Tests.Common; @@ -504,6 +505,52 @@ namespace Microsoft.AspNetCore.SignalR.Tests } } + [Fact] + public async Task HubMethodCanBeRenamedWithAttribute() + { + var serviceProvider = CreateServiceProvider(); + + var endPoint = serviceProvider.GetService>(); + + using (var client = new TestClient()) + { + var endPointTask = endPoint.OnConnectedAsync(client.Connection); + + var result = (await client.InvokeAsync("RenamedMethod").OrTimeout()).Result; + + // json serializer makes this a long + Assert.Equal(43L, result); + + // kill the connection + client.Dispose(); + + await endPointTask.OrTimeout(); + } + } + + [Fact] + public async Task HubMethodNameAttributeIsInherited() + { + var serviceProvider = CreateServiceProvider(); + + var endPoint = serviceProvider.GetService>(); + + using (var client = new TestClient()) + { + var endPointTask = endPoint.OnConnectedAsync(client.Connection); + + var result = (await client.InvokeAsync("RenamedVirtualMethod").OrTimeout()).Result; + + // json serializer makes this a long + Assert.Equal(34L, result); + + // kill the connection + client.Dispose(); + + await endPointTask.OrTimeout(); + } + } + [Theory] [InlineData(nameof(MethodHub.VoidMethod))] [InlineData(nameof(MethodHub.MethodThatThrows))] @@ -1747,6 +1794,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests return 43; } + [HubMethodName("RenamedMethod")] + public int ATestMethodThatIsRenamedByTheAttribute() + { + return 43; + } + public string Echo(string data) { return data; @@ -1807,6 +1860,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests { return num - 10; } + + public override int VirtualMethodRenamed() + { + return 34; + } } private class BaseHub : TestHub @@ -1820,6 +1878,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests { return num; } + + [HubMethodName("RenamedVirtualMethod")] + public virtual int VirtualMethodRenamed() + { + return 43; + } } private class InvalidHub : TestHub