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
                    
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="CreateIfLabs.AspNetCore.SignalR.Client" Version="0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CreateIfLabs.AspNetCore.SignalR.Client" Version="0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="CreateIfLabs.AspNetCore.SignalR.Client" />
                    
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 CreateIfLabs.AspNetCore.SignalR.Client --version 0.0.2
                    
#r "nuget: CreateIfLabs.AspNetCore.SignalR.Client, 0.0.2"
                    
#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 CreateIfLabs.AspNetCore.SignalR.Client@0.0.2
                    
#: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=CreateIfLabs.AspNetCore.SignalR.Client&version=0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=CreateIfLabs.AspNetCore.SignalR.Client&version=0.0.2
                    
Install as a Cake Tool

🛰️ 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 via RegisterInterfaceHandler).

  • TServer → defines server-side methods that the client can invoke
    (used in calls like InvokeAsync<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 internal HubConnection.
  • 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 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. 
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
0.0.2 171 2/20/2026
0.0.1 289 10/12/2025