VOOZH about

URL: https://www.nuget.org/packages/Open.SignalR.SharedClient/

⇱ NuGet Gallery | Open.SignalR.SharedClient 1.2.0




👁 Image
Open.SignalR.SharedClient 1.2.0

dotnet add package Open.SignalR.SharedClient --version 1.2.0
 
 
NuGet\Install-Package Open.SignalR.SharedClient -Version 1.2.0
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Open.SignalR.SharedClient" Version="1.2.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Open.SignalR.SharedClient" Version="1.2.0" />
 
Directory.Packages.props
<PackageReference Include="Open.SignalR.SharedClient" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Open.SignalR.SharedClient --version 1.2.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Open.SignalR.SharedClient, 1.2.0"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Open.SignalR.SharedClient@1.2.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Open.SignalR.SharedClient&version=1.2.0
 
Install as a Cake Addin
#tool nuget:?package=Open.SignalR.SharedClient&version=1.2.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Open.SignalR.SharedClient

👁 NuGet

A scoped hub connection and provider for easily sharing SignalR connections.

IScopedHubConnection

Provides 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.

Usage

DI Setup

Default

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.

With Named Connections

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.

With Simple URL Mapping
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);
}
Custom DI Config
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)
		});

Example

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

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