![]() |
VOOZH | about |
dotnet add package Open.SignalR.SharedClient --version 1.2.0
NuGet\Install-Package Open.SignalR.SharedClient -Version 1.2.0
<PackageReference Include="Open.SignalR.SharedClient" Version="1.2.0" />
<PackageVersion Include="Open.SignalR.SharedClient" Version="1.2.0" />Directory.Packages.props
<PackageReference Include="Open.SignalR.SharedClient" />Project file
paket add Open.SignalR.SharedClient --version 1.2.0
#r "nuget: Open.SignalR.SharedClient, 1.2.0"
#:package Open.SignalR.SharedClient@1.2.0
#addin nuget:?package=Open.SignalR.SharedClient&version=1.2.0Install as a Cake Addin
#tool nuget:?package=Open.SignalR.SharedClient&version=1.2.0Install as a Cake Tool
A scoped hub connection and provider for easily sharing SignalR connections.
IScopedHubConnectionProvides all the same functionality as a HubConnection,
but does not interfere with other IScopedHubConnection instances.
Disposing the scoped connection cleans up all the handlers but leaves the connection intact for others to use.
The following adds a default provider as a singleton to the service collection.
Assumes the consumers will know which URL to ask for.
services.AddScopedHubConnectionProvider();
Consumers use IScopedHubConnectionProvider.GetConnectionFor(url) to get a hub connection.
Consumers use INamedScopedHubConnectionFactory.GetGonnectionByName(hubName) to get a hub connection.
This is necessary to ensure NavigationManager or other scope isolated services/resources are not used in a singleton context.
Note: Client/Server hybrid apps will need to replicate the DI setup on the server side.
public static class AppServicesConfiguration
{
/// <summary>
/// Provided to keep the client and server DI in sync.
/// </summary>
public static IServiceCollection AddAppHubConnections(this IServiceCollection services)
=> services
.AddScopedHubConnectionProvider()
.AddNamedScopedHubConnectionMapping(serviceProvider => hubName => hubName switch
{
"hub1" => serviceProvider.ToAbsoluteUri("/hub/hub1"),
"counter" => serviceProvider.ToAbsoluteUri("/hub/counter"),
_ => null, // Returning null will signal that the name was not found and throw appropritately.
});
/// <summary>
/// Shortcut for <see cref="NavigationManager"/>.
/// </summary>
public static Uri ToAbsoluteUri(this IServiceProvider sp, string path)
=> sp.GetRequiredService<NavigationManager>().ToAbsoluteUri(path);
}
services
.AddScopedHubConnectionProvider()
.AddNamedScopedHubConnectionFactory(serviceProvider => hubName => hubName switch
{
// With Automatic Reconnnect
"hub1" => (serviceProvider.ToAbsoluteUri("/hub/hub1"),
uri => new HubConnectionBuilder().WithAutomaticReconnect().WithUrl(uri)
),
// With Stateful Reconnect
"hub2" => (serviceProvider.ToAbsoluteUri("/hub/hub1"),
uri => new HubConnectionBuilder().WithStatefulReconnect().WithUrl(uri)
),
_ => (null, null)
});
public class MyService : IDisposable
{
private readonly IScopedHubConnection _hub;
public MyService(INamedScopedHubConnectionFactory connectionProvider)
{
_hub = connectionProvider.GetConnectionByNam("hub1");
_hub.On("MyMethod1", (string arg1, string arg2) =>
{
// Do something when the method is invoked on the hub.
});
_hub.On("MyMethod2", (string arg1, string arg2) =>
{
// Do something when the method is invoked on the hub.
});
// Automatically invokes OnConnectionEstablished when a connnection is available
// and invokes it on every reconnection.
_hub.OnConnected(OnConnectionEstablished);
}
public void Dispose() => _hub.Dispose();
static void OnConnectionEstablished(IScopedHubConnection hub)
{
hub.SendAsync("SubTo", "MyMethod1");
hub.SendAsync("SubTo", "MyMethod2");
}
public async Task DoSomething()
{
// Automatically guarantees a connection and invokes the method.
await _hub.InvokeAsync("MyMethod", "arg1", "arg2");
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.0 | 225 | 2/13/2025 |
| 1.1.0 | 205 | 2/11/2025 |
| 1.0.3 | 201 | 2/9/2025 |
| 1.0.2-preview | 185 | 2/9/2025 |
| 1.0.1-preview | 192 | 2/9/2025 |
| 1.0.0-preview | 160 | 2/8/2025 |