Skip to content

Commit 65276b0

Browse files
Add base interface for IHubContext (#33443)
1 parent bc9383e commit 65276b0

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/SignalR/server/Core/src/IHubContext.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.SignalR
66
/// <summary>
77
/// A context abstraction for a hub.
88
/// </summary>
9-
public interface IHubContext<THub> where THub : Hub
9+
public interface IHubContext
1010
{
1111
/// <summary>
1212
/// Gets a <see cref="IHubClients"/> that can be used to invoke methods on clients connected to the hub.
@@ -18,4 +18,11 @@ public interface IHubContext<THub> where THub : Hub
1818
/// </summary>
1919
IGroupManager Groups { get; }
2020
}
21+
22+
/// <summary>
23+
/// A context abstraction for a hub.
24+
/// </summary>
25+
public interface IHubContext<out THub> : IHubContext where THub : Hub
26+
{
27+
}
2128
}

src/SignalR/server/Core/src/PublicAPI.Unshipped.txt

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
*REMOVED*abstract Microsoft.AspNetCore.SignalR.HubLifetimeManager<THub>.SendUsersAsync(System.Collections.Generic.IReadOnlyList<string!>! userIds, string! methodName, object?[]? args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
2222
*REMOVED*Microsoft.AspNetCore.SignalR.IClientProxy.SendCoreAsync(string! method, object?[]? args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
2323
*REMOVED*virtual Microsoft.AspNetCore.SignalR.HubConnectionContext.User.get -> System.Security.Claims.ClaimsPrincipal?
24+
*REMOVED*Microsoft.AspNetCore.SignalR.IHubContext<THub>.Clients.get -> Microsoft.AspNetCore.SignalR.IHubClients!
25+
*REMOVED*Microsoft.AspNetCore.SignalR.IHubContext<THub>.Groups.get -> Microsoft.AspNetCore.SignalR.IGroupManager!
26+
Microsoft.AspNetCore.SignalR.IHubContext
27+
Microsoft.AspNetCore.SignalR.IHubContext.Clients.get -> Microsoft.AspNetCore.SignalR.IHubClients!
28+
Microsoft.AspNetCore.SignalR.IHubContext.Groups.get -> Microsoft.AspNetCore.SignalR.IGroupManager!
2429
virtual Microsoft.AspNetCore.SignalR.HubConnectionContext.User.get -> System.Security.Claims.ClaimsPrincipal!
2530
override Microsoft.AspNetCore.SignalR.DefaultHubLifetimeManager<THub>.SendAllAsync(string! methodName, object?[]! args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
2631
override Microsoft.AspNetCore.SignalR.DefaultHubLifetimeManager<THub>.SendAllExceptAsync(string! methodName, object?[]! args, System.Collections.Generic.IReadOnlyList<string!>! excludedConnectionIds, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!

src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs

+54
Original file line numberDiff line numberDiff line change
@@ -4500,6 +4500,60 @@ public async Task SpecificHubOptionForMaximumReceiveMessageSizeIsUsedOverGlobalH
45004500
}
45014501
}
45024502

4503+
[Fact]
4504+
public async Task CanSendThroughIHubContext()
4505+
{
4506+
using (StartVerifiableLog())
4507+
{
4508+
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(null, LoggerFactory);
4509+
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();
4510+
4511+
using var client = new TestClient();
4512+
4513+
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
4514+
4515+
// Wait for a connection, or for the endpoint to fail.
4516+
await client.Connected.OrThrowIfOtherFails(connectionHandlerTask).DefaultTimeout();
4517+
4518+
IHubContext context = serviceProvider.GetRequiredService<IHubContext<MethodHub>>();
4519+
await context.Clients.All.SendAsync("Send", "test");
4520+
4521+
var message = await client.ReadAsync().DefaultTimeout();
4522+
var invocation = Assert.IsType<InvocationMessage>(message);
4523+
4524+
Assert.Single(invocation.Arguments);
4525+
Assert.Equal("test", invocation.Arguments[0]);
4526+
Assert.Equal("Send", invocation.Target);
4527+
}
4528+
}
4529+
4530+
[Fact]
4531+
public async Task CanSendThroughIHubContextBaseHub()
4532+
{
4533+
using (StartVerifiableLog())
4534+
{
4535+
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(null, LoggerFactory);
4536+
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();
4537+
4538+
using var client = new TestClient();
4539+
4540+
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
4541+
4542+
// Wait for a connection, or for the endpoint to fail.
4543+
await client.Connected.OrThrowIfOtherFails(connectionHandlerTask).DefaultTimeout();
4544+
4545+
IHubContext<TestHub> context = serviceProvider.GetRequiredService<IHubContext<MethodHub>>();
4546+
await context.Clients.All.SendAsync("Send", "test");
4547+
4548+
var message = await client.ReadAsync().DefaultTimeout();
4549+
var invocation = Assert.IsType<InvocationMessage>(message);
4550+
4551+
Assert.Single(invocation.Arguments);
4552+
Assert.Equal("test", invocation.Arguments[0]);
4553+
Assert.Equal("Send", invocation.Target);
4554+
}
4555+
}
4556+
45034557
private class CustomHubActivator<THub> : IHubActivator<THub> where THub : Hub
45044558
{
45054559
public int ReleaseCount;

0 commit comments

Comments
 (0)