fix #625 by implementing HubMethodNameAttribute (#1173)

This commit is contained in:
Andrew Stanton-Nurse 2017-12-01 09:48:26 -08:00 committed by GitHub
parent db511a036d
commit 3005337a9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 1 deletions

View File

@ -598,7 +598,9 @@ namespace Microsoft.AspNetCore.SignalR
foreach (var methodInfo in HubReflectionHelper.GetHubMethods(hubType))
{
var methodName = methodInfo.Name;
var methodName =
methodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name ??
methodInfo.Name;
if (_methods.ContainsKey(methodName))
{

View File

@ -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;
}
}
}

View File

@ -3,6 +3,7 @@
<PropertyGroup>
<Description>Real-time communication framework for ASP.NET Core.</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Microsoft.AspNetCore.SignalR</RootNamespace>
</PropertyGroup>
<ItemGroup>

View File

@ -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<HubEndPoint<MethodHub>>();
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<HubEndPoint<InheritedHub>>();
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