Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
fix #625 by implementing HubMethodNameAttribute (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
analogrelay authored Dec 1, 2017
1 parent db511a0 commit 3005337
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Microsoft.AspNetCore.SignalR.Core/HubEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,9 @@ private void DiscoverHubMethods()

foreach (var methodInfo in HubReflectionHelper.GetHubMethods(hubType))
{
var methodName = methodInfo.Name;
var methodName =
methodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name ??
methodInfo.Name;

if (_methods.ContainsKey(methodName))
{
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.AspNetCore.SignalR.Core/HubMethodNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
64 changes: 64 additions & 0 deletions test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -504,6 +505,52 @@ public async Task HubMethodCanBeVoid()
}
}

[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))]
Expand Down Expand Up @@ -1747,6 +1794,12 @@ public int ValueMethod()
return 43;
}

[HubMethodName("RenamedMethod")]
public int ATestMethodThatIsRenamedByTheAttribute()
{
return 43;
}

public string Echo(string data)
{
return data;
Expand Down Expand Up @@ -1807,6 +1860,11 @@ public override int VirtualMethod(int num)
{
return num - 10;
}

public override int VirtualMethodRenamed()
{
return 34;
}
}

private class BaseHub : TestHub
Expand All @@ -1820,6 +1878,12 @@ public virtual int VirtualMethod(int num)
{
return num;
}

[HubMethodName("RenamedVirtualMethod")]
public virtual int VirtualMethodRenamed()
{
return 43;
}
}

private class InvalidHub : TestHub
Expand Down

0 comments on commit 3005337

Please sign in to comment.