Add support custom client hub method name (#23614)

This commit is contained in:
Vasilev Pyotr 2020-07-10 03:58:47 +03:00 committed by GitHub
parent 3192da4443
commit 5d170de769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -144,6 +144,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal
paramTypes = paramTypes.Take(paramTypes.Length - 1).ToArray();
}
var methodName =
interfaceMethodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name ??
interfaceMethodInfo.Name;
var generator = methodBuilder.GetILGenerator();
// Declare local variable to store the arguments to IClientProxy.SendCoreAsync
@ -154,7 +158,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal
generator.Emit(OpCodes.Ldfld, proxyField);
// The first argument to IClientProxy.SendCoreAsync is this method's name
generator.Emit(OpCodes.Ldstr, interfaceMethodInfo.Name);
generator.Emit(OpCodes.Ldstr, methodName);
// Create an new object array to hold all the parameters to this method
generator.Emit(OpCodes.Ldc_I4, paramTypes.Length); // Stack:

View File

@ -38,6 +38,30 @@ namespace Microsoft.AspNetCore.SignalR.Tests.Internal
await task.OrTimeout();
}
[Fact]
public async Task ProducesImplementationThatProxiesMethodsToIRenamedClientProxyAsync()
{
var clientProxy = new MockProxy();
var typedProxy = TypedClientBuilder<IRenamedTestClient>.Build(clientProxy);
var objArg = new object();
var task = typedProxy.MethodAsync("foo", 42, objArg);
Assert.False(task.IsCompleted);
Assert.Collection(clientProxy.Sends,
send =>
{
Assert.Equal("Method", send.Method);
Assert.Equal("foo", send.Arguments[0]);
Assert.Equal(42, send.Arguments[1]);
Assert.Equal(CancellationToken.None, send.CancellationToken);
Assert.Same(objArg, send.Arguments[2]);
send.Complete();
});
await task.OrTimeout();
}
[Fact]
public async Task SupportsSubInterfaces()
{
@ -189,6 +213,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests.Internal
Task Method(string arg1, int arg2, object arg3);
}
public interface IRenamedTestClient
{
[HubMethodName("Method")]
Task MethodAsync(string arg1, int arg2, object arg3);
}
public interface IVoidMethodClient
{
void Method(string arg1, int arg2, object arg3);