CreateIfLabs.AspNetCore.SignalR.Client
0.0.2
dotnet add package CreateIfLabs.AspNetCore.SignalR.Client --version 0.0.2
NuGet\Install-Package CreateIfLabs.AspNetCore.SignalR.Client -Version 0.0.2
<PackageReference Include="CreateIfLabs.AspNetCore.SignalR.Client" Version="0.0.2" />
<PackageVersion Include="CreateIfLabs.AspNetCore.SignalR.Client" Version="0.0.2" />
<PackageReference Include="CreateIfLabs.AspNetCore.SignalR.Client" />
paket add CreateIfLabs.AspNetCore.SignalR.Client --version 0.0.2
#r "nuget: CreateIfLabs.AspNetCore.SignalR.Client, 0.0.2"
#:package CreateIfLabs.AspNetCore.SignalR.Client@0.0.2
#addin nuget:?package=CreateIfLabs.AspNetCore.SignalR.Client&version=0.0.2
#tool nuget:?package=CreateIfLabs.AspNetCore.SignalR.Client&version=0.0.2
🛰️ CreateIfLabs.AspNetCore.SignalR.Client
Type-safe, dependency injection–friendly SignalR client with support for multiple endpoints
(using Named Options and Keyed Services, available in .NET 8).
🚀 Installation
Add the project or NuGet package to your application:
dotnet add package CreateIfLabs.AspNetCore.SignalR.Client
⚙️ Configuration & Registration
Register your client inside Program.cs (or Startup.cs).
🔹 Single Client (one hub)
using CreateIfLabs.AspNetCore.SignalR.Client;
builder.Services.AddSignalRConnector<IMyHub>(options =>
{
options
.WithUrl("https://example.com/myhub")
.WithApiKey("super-secret")
.WithAutomaticReconnect();
});
This automatically registers a SignalRClient<IMyHub> in the DI container,
which you can inject directly into your services or hosted workers.
public class Worker
{
private readonly SignalRClient<IMyHub> _hubClient;
public Worker(SignalRClient<IMyHub> hubClient)
{
_hubClient = hubClient;
}
public async Task RunAsync()
{
await _hubClient.ConnectAsync();
// Example: invoke a server-side method "Ping"
await _hubClient.InvokeAsync<IMyHubServer>(x => x.Ping());
await _hubClient.DisconnectAsync();
}
}
🔹 Multiple Hubs / Endpoints (Named Options + Keyed Services, .NET 8)
To connect to multiple different endpoints:
builder.Services.AddSignalRConnector<IMyHub>("hubA", options =>
{
options
.WithUrl("https://api.example.com/hubA")
.WithApiKey("key-A")
.WithAutomaticReconnect();
});
builder.Services.AddSignalRConnector<IMyHub>("hubB", options =>
{
options
.WithUrl("https://api.example.com/hubB")
.WithApiKey("key-B");
});
Then inject the specific clients by key:
using Microsoft.Extensions.DependencyInjection;
public class Worker
{
private readonly SignalRClient<IMyHub> _hubA;
private readonly SignalRClient<IMyHub> _hubB;
public Worker(
[FromKeyedServices("hubA")] SignalRClient<IMyHub> hubA,
[FromKeyedServices("hubB")] SignalRClient<IMyHub> hubB)
{
_hubA = hubA;
_hubB = hubB;
}
public async Task RunAsync()
{
await _hubA.ConnectAsync();
await _hubB.ConnectAsync();
await _hubA.InvokeAsync<IMyHubServer>(x => x.Notify("Hello Hub A!"));
await _hubB.InvokeAsync<IMyHubServer>(x => x.Notify("Hello Hub B!"));
}
}
Or resolve manually via IServiceProvider:
var hubA = provider.GetRequiredKeyedService<SignalRClient<IMyHub>>("hubA");
var hubB = provider.GetRequiredKeyedService<SignalRClient<IMyHub>>("hubB");
🧩 Interface Structure
TInterface→ defines client handler methods that the server can call
(these are automatically registered viaRegisterInterfaceHandler).TServer→ defines server-side methods that the client can invoke
(used in calls likeInvokeAsync<TServer>).
Example:
public interface IMyHub
{
// Called from the server
Task DeviceConnected(string id);
}
public interface IMyHubServer
{
// Called from the client
Task Notify(string message);
Task<int> Add(int a, int b);
}
Usage:
await client.InvokeAsync<IMyHubServer>(x => x.Notify("Hello!"));
var sum = await client.InvokeAsync<IMyHubServer, int>(x => x.Add(2, 3));
🧠 Lifecycle
- Each
SignalRClient<T>instance is a singleton managing its internalHubConnection. - Call
ConnectAsync()once at startup or on-demand. - Call
DisconnectAsync()during shutdown or when manually reconnecting.
🧰 Complete Example
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalRConnector<IMyHub>(o =>
{
o.WithUrl("https://localhost:5001/hub")
.WithApiKey("demo-key");
});
builder.Services.AddHostedService<Worker>();
var app = builder.Build();
await app.RunAsync();
public class Worker : IHostedService
{
private readonly SignalRClient<IMyHub> _client;
public Worker(SignalRClient<IMyHub> client) => _client = client;
public async Task StartAsync(CancellationToken ct)
{
await _client.ConnectAsync(ct);
await _client.InvokeAsync<IMyHubServer>(x => x.Notify("Worker started!"));
}
public Task StopAsync(CancellationToken ct) => _client.DisconnectAsync(ct);
}
✅ Done:
With this setup you can register and configure any number of SignalR clients,
all fully type-safe and DI-friendly — clean, composable, and testable.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.SignalR.Client (>= 10.0.3)
- Microsoft.AspNetCore.SignalR.Client.Core (>= 10.0.3)
-
net8.0
- Microsoft.AspNetCore.SignalR.Client (>= 10.0.3)
- Microsoft.AspNetCore.SignalR.Client.Core (>= 10.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.