Skip to content

Add base interface for IHubContext #33443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/SignalR/server/Core/src/IHubContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.SignalR
/// <summary>
/// A context abstraction for a hub.
/// </summary>
public interface IHubContext<THub> where THub : Hub
public interface IHubContext
{
/// <summary>
/// Gets a <see cref="IHubClients"/> that can be used to invoke methods on clients connected to the hub.
Expand All @@ -18,4 +18,11 @@ public interface IHubContext<THub> where THub : Hub
/// </summary>
IGroupManager Groups { get; }
}

/// <summary>
/// A context abstraction for a hub.
/// </summary>
public interface IHubContext<out THub> : IHubContext where THub : Hub
{
}
}
5 changes: 5 additions & 0 deletions src/SignalR/server/Core/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*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!
*REMOVED*Microsoft.AspNetCore.SignalR.IClientProxy.SendCoreAsync(string! method, object?[]? args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
*REMOVED*virtual Microsoft.AspNetCore.SignalR.HubConnectionContext.User.get -> System.Security.Claims.ClaimsPrincipal?
*REMOVED*Microsoft.AspNetCore.SignalR.IHubContext<THub>.Clients.get -> Microsoft.AspNetCore.SignalR.IHubClients!
*REMOVED*Microsoft.AspNetCore.SignalR.IHubContext<THub>.Groups.get -> Microsoft.AspNetCore.SignalR.IGroupManager!
Microsoft.AspNetCore.SignalR.IHubContext
Microsoft.AspNetCore.SignalR.IHubContext.Clients.get -> Microsoft.AspNetCore.SignalR.IHubClients!
Microsoft.AspNetCore.SignalR.IHubContext.Groups.get -> Microsoft.AspNetCore.SignalR.IGroupManager!
virtual Microsoft.AspNetCore.SignalR.HubConnectionContext.User.get -> System.Security.Claims.ClaimsPrincipal!
override Microsoft.AspNetCore.SignalR.DefaultHubLifetimeManager<THub>.SendAllAsync(string! methodName, object?[]! args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
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!
Expand Down
54 changes: 54 additions & 0 deletions src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4500,6 +4500,60 @@ public async Task SpecificHubOptionForMaximumReceiveMessageSizeIsUsedOverGlobalH
}
}

[Fact]
public async Task CanSendThroughIHubContext()
{
using (StartVerifiableLog())
{
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(null, LoggerFactory);
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();

using var client = new TestClient();

var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

// Wait for a connection, or for the endpoint to fail.
await client.Connected.OrThrowIfOtherFails(connectionHandlerTask).DefaultTimeout();

IHubContext context = serviceProvider.GetRequiredService<IHubContext<MethodHub>>();
await context.Clients.All.SendAsync("Send", "test");

var message = await client.ReadAsync().DefaultTimeout();
var invocation = Assert.IsType<InvocationMessage>(message);

Assert.Single(invocation.Arguments);
Assert.Equal("test", invocation.Arguments[0]);
Assert.Equal("Send", invocation.Target);
}
}

[Fact]
public async Task CanSendThroughIHubContextBaseHub()
{
using (StartVerifiableLog())
{
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(null, LoggerFactory);
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();

using var client = new TestClient();

var connectionHandlerTask = await client.ConnectAsync(connectionHandler);

// Wait for a connection, or for the endpoint to fail.
await client.Connected.OrThrowIfOtherFails(connectionHandlerTask).DefaultTimeout();

IHubContext<TestHub> context = serviceProvider.GetRequiredService<IHubContext<MethodHub>>();
await context.Clients.All.SendAsync("Send", "test");

var message = await client.ReadAsync().DefaultTimeout();
var invocation = Assert.IsType<InvocationMessage>(message);

Assert.Single(invocation.Arguments);
Assert.Equal("test", invocation.Arguments[0]);
Assert.Equal("Send", invocation.Target);
}
}

private class CustomHubActivator<THub> : IHubActivator<THub> where THub : Hub
{
public int ReleaseCount;
Expand Down