parent
db511a036d
commit
3005337a9c
|
|
@ -598,7 +598,9 @@ namespace Microsoft.AspNetCore.SignalR
|
||||||
|
|
||||||
foreach (var methodInfo in HubReflectionHelper.GetHubMethods(hubType))
|
foreach (var methodInfo in HubReflectionHelper.GetHubMethods(hubType))
|
||||||
{
|
{
|
||||||
var methodName = methodInfo.Name;
|
var methodName =
|
||||||
|
methodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name ??
|
||||||
|
methodInfo.Name;
|
||||||
|
|
||||||
if (_methods.ContainsKey(methodName))
|
if (_methods.ContainsKey(methodName))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>Real-time communication framework for ASP.NET Core.</Description>
|
<Description>Real-time communication framework for ASP.NET Core.</Description>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<RootNamespace>Microsoft.AspNetCore.SignalR</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ using System.Threading.Channels;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.SignalR.Core;
|
||||||
using Microsoft.AspNetCore.SignalR.Internal;
|
using Microsoft.AspNetCore.SignalR.Internal;
|
||||||
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
|
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
|
||||||
using Microsoft.AspNetCore.SignalR.Tests.Common;
|
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]
|
[Theory]
|
||||||
[InlineData(nameof(MethodHub.VoidMethod))]
|
[InlineData(nameof(MethodHub.VoidMethod))]
|
||||||
[InlineData(nameof(MethodHub.MethodThatThrows))]
|
[InlineData(nameof(MethodHub.MethodThatThrows))]
|
||||||
|
|
@ -1747,6 +1794,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
return 43;
|
return 43;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HubMethodName("RenamedMethod")]
|
||||||
|
public int ATestMethodThatIsRenamedByTheAttribute()
|
||||||
|
{
|
||||||
|
return 43;
|
||||||
|
}
|
||||||
|
|
||||||
public string Echo(string data)
|
public string Echo(string data)
|
||||||
{
|
{
|
||||||
return data;
|
return data;
|
||||||
|
|
@ -1807,6 +1860,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
{
|
{
|
||||||
return num - 10;
|
return num - 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override int VirtualMethodRenamed()
|
||||||
|
{
|
||||||
|
return 34;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BaseHub : TestHub
|
private class BaseHub : TestHub
|
||||||
|
|
@ -1820,6 +1878,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
{
|
{
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HubMethodName("RenamedVirtualMethod")]
|
||||||
|
public virtual int VirtualMethodRenamed()
|
||||||
|
{
|
||||||
|
return 43;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class InvalidHub : TestHub
|
private class InvalidHub : TestHub
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue