[SignalR] Replace Single(connectionId) with Client(connectionId) and Caller#42236
Merged
Conversation
BrennanConroy
approved these changes
Jun 16, 2022
- Yes this means there are probably a fair number of other tests like it. - We definitely will need a breaking change announcement if we merge this.
Member
Made a 6.0 app and published it, then ran the published app against a custom 7.0 runtime with these changes. Nothing broke as expected. Below is the rough code used. var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(typeof(IHubContext<>), typeof(MyHubContext<>));
builder.Services.AddSignalR();
var app = builder.Build();
app.MapGet("/custom", (IHubContext<MyHub> c, [FromQuery] string id) =>
{
c.Clients.Client(id).SendAsync("ReceiveMessage", "test");
});
app.MapHub<MyHub>("/hub");
app.Run();
public class MyHub : Hub { }
public class MyHubContext<THub> : IHubContext<THub> where THub : Hub
{
private readonly HubLifetimeManager<THub> _lm;
public MyHubContext(HubLifetimeManager<THub> lm)
{
_lm = lm;
}
public IHubClients Clients => new HubClient<THub>(_lm);
public IGroupManager Groups => throw new NotImplementedException();
}
internal class HubClient<THub> : IHubClients where THub : Hub
{
private readonly HubLifetimeManager<THub> _lm;
public HubClient(HubLifetimeManager<THub> lm)
{
_lm = lm;
}
public IClientProxy Client(string connectionId)
{
return new SingleClientProxy(_lm, connectionId);
}
// all the interface methods with NotImplementedException, removed for brevity
internal sealed class SingleClientProxy : IClientProxy
{
private readonly string _connectionId;
private readonly HubLifetimeManager<THub> _lifetimeManager;
public SingleClientProxy(HubLifetimeManager<THub> lifetimeManager, string connectionId)
{
_lifetimeManager = lifetimeManager;
_connectionId = connectionId;
}
public Task SendCoreAsync(string method, object?[] args, CancellationToken cancellationToken = default)
{
return _lifetimeManager.SendConnectionAsync(_connectionId, method, args, cancellationToken);
}
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We recently discovered can add
newCallerandClientmembers without breaking existingIHubClientsandIHubCallerClientsimplementations unless they're being used for client results which never worked before .NET 7 making the change both non-binary and non-source breaking[1].Before
After
Fixes #41994
IHubClientsimplementation built against .NET 6.