Skip to content

Commit a8c39e9

Browse files
Provide default implementation for IHubFilter.InvokeMethodAsync (#23968)
1 parent e7b55df commit a8c39e9

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/SignalR/server/Core/src/IHubFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IHubFilter
1919
/// <param name="invocationContext">The context for the method invocation that holds all the important information about the invoke.</param>
2020
/// <param name="next">The next filter to run, and for the final one, the Hub invocation.</param>
2121
/// <returns>Returns the result of the Hub method invoke.</returns>
22-
ValueTask<object> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object>> next);
22+
ValueTask<object> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object>> next) => next(invocationContext);
2323

2424
/// <summary>
2525
/// Allows handling of the <see cref="Hub.OnConnectedAsync"/> method.

src/SignalR/server/SignalR/test/HubFilterTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Diagnostics;
65
using System.Threading.Tasks;
76
using Microsoft.AspNetCore.Internal;
87
using Microsoft.Extensions.DependencyInjection;
@@ -136,6 +135,40 @@ private async Task AssertMethodsCalled(IServiceProvider serviceProvider, TcsServ
136135
}
137136
}
138137

138+
[Fact]
139+
public async Task HubFilterDoesNotNeedToImplementMethods()
140+
{
141+
using (StartVerifiableLog())
142+
{
143+
var tcsService = new TcsService();
144+
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(services =>
145+
{
146+
services.AddSignalR().AddHubOptions<DynamicTestHub>(options =>
147+
{
148+
options.AddFilter(typeof(EmptyFilter));
149+
});
150+
}, LoggerFactory);
151+
152+
153+
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<DynamicTestHub>>();
154+
155+
using (var client = new TestClient())
156+
{
157+
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
158+
159+
await client.Connected.OrTimeout();
160+
161+
var completion = await client.InvokeAsync(nameof(DynamicTestHub.Echo), "hello");
162+
Assert.Null(completion.Error);
163+
Assert.Equal("hello", completion.Result);
164+
165+
client.Dispose();
166+
167+
await connectionHandlerTask.OrTimeout();
168+
}
169+
}
170+
}
171+
139172
[Fact]
140173
public async Task MutlipleFilters_MethodsAreCalled()
141174
{

src/SignalR/server/SignalR/test/TestFilters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,9 @@ public ValueTask<object> InvokeMethodAsync(HubInvocationContext invocationContex
233233
return next(context);
234234
}
235235
}
236+
237+
public class EmptyFilter : IHubFilter
238+
{
239+
// Purposefully not implementing any methods
240+
}
236241
}

0 commit comments

Comments
 (0)