Skip to content

Commit 5d170de

Browse files
authored
Add support custom client hub method name (#23614)
1 parent 3192da4 commit 5d170de

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/SignalR/server/Core/src/Internal/TypedClientBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ private static void BuildMethod(TypeBuilder type, MethodInfo interfaceMethodInfo
144144
paramTypes = paramTypes.Take(paramTypes.Length - 1).ToArray();
145145
}
146146

147+
var methodName =
148+
interfaceMethodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name ??
149+
interfaceMethodInfo.Name;
150+
147151
var generator = methodBuilder.GetILGenerator();
148152

149153
// Declare local variable to store the arguments to IClientProxy.SendCoreAsync
@@ -154,7 +158,7 @@ private static void BuildMethod(TypeBuilder type, MethodInfo interfaceMethodInfo
154158
generator.Emit(OpCodes.Ldfld, proxyField);
155159

156160
// The first argument to IClientProxy.SendCoreAsync is this method's name
157-
generator.Emit(OpCodes.Ldstr, interfaceMethodInfo.Name);
161+
generator.Emit(OpCodes.Ldstr, methodName);
158162

159163
// Create an new object array to hold all the parameters to this method
160164
generator.Emit(OpCodes.Ldc_I4, paramTypes.Length); // Stack:

src/SignalR/server/SignalR/test/Internal/TypedClientBuilderTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ public async Task ProducesImplementationThatProxiesMethodsToIClientProxyAsync()
3838
await task.OrTimeout();
3939
}
4040

41+
[Fact]
42+
public async Task ProducesImplementationThatProxiesMethodsToIRenamedClientProxyAsync()
43+
{
44+
var clientProxy = new MockProxy();
45+
var typedProxy = TypedClientBuilder<IRenamedTestClient>.Build(clientProxy);
46+
47+
var objArg = new object();
48+
var task = typedProxy.MethodAsync("foo", 42, objArg);
49+
Assert.False(task.IsCompleted);
50+
51+
Assert.Collection(clientProxy.Sends,
52+
send =>
53+
{
54+
Assert.Equal("Method", send.Method);
55+
Assert.Equal("foo", send.Arguments[0]);
56+
Assert.Equal(42, send.Arguments[1]);
57+
Assert.Equal(CancellationToken.None, send.CancellationToken);
58+
Assert.Same(objArg, send.Arguments[2]);
59+
send.Complete();
60+
});
61+
62+
await task.OrTimeout();
63+
}
64+
4165
[Fact]
4266
public async Task SupportsSubInterfaces()
4367
{
@@ -189,6 +213,12 @@ public interface ITestClient
189213
Task Method(string arg1, int arg2, object arg3);
190214
}
191215

216+
public interface IRenamedTestClient
217+
{
218+
[HubMethodName("Method")]
219+
Task MethodAsync(string arg1, int arg2, object arg3);
220+
}
221+
192222
public interface IVoidMethodClient
193223
{
194224
void Method(string arg1, int arg2, object arg3);

0 commit comments

Comments
 (0)