Pandax.SignalRClient 1.0.0

dotnet add package Pandax.SignalRClient --version 1.0.0
                    
NuGet\Install-Package Pandax.SignalRClient -Version 1.0.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="Pandax.SignalRClient" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pandax.SignalRClient" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Pandax.SignalRClient" />
                    
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 Pandax.SignalRClient --version 1.0.0
                    
#r "nuget: Pandax.SignalRClient, 1.0.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 Pandax.SignalRClient@1.0.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=Pandax.SignalRClient&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Pandax.SignalRClient&version=1.0.0
                    
Install as a Cake Tool

Pandax.SignalRClient

SignalRClient is a lightweight wrapper around the ASP.NET Core SignalR client for building resilient .NET consumers.

NuGet package: Pandax.SignalRClient

中文文档

Features

  • Wraps SignalR client connection setup into a reusable base client
  • Supports access token injection through a custom authentication provider
  • Supports configurable retry policies for reconnect behavior
  • Works well in background services and long-running worker processes

Target Framework

  • .NET 9.0

Installation

dotnet add package Pandax.SignalRClient

Core Types

  • BaseSignalRClient<TOptions>: the base class that creates and manages HubConnection
  • SignalRClientOptions: base options with Url and RetryPolicy
  • IAuthenticationProvider: abstraction used to provide an access token for SignalR requests
  • DefaultRetryPolicy: built-in retry policy that retries 5 times with a 5 second interval

Quick Start

The recommended integration pattern is:

  1. Implement IAuthenticationProvider
  2. Create your options type derived from SignalRClientOptions
  3. Create a client class derived from BaseSignalRClient<TOptions>
  4. Register the client in dependency injection
  5. Start it from a hosted service or your application entry point

1. Implement an authentication provider

using Pandax.SignalRClient;

public sealed class JwtAuthenticationProvider : IAuthenticationProvider
{
    public Task<string?> GetAccessTokenAsync()
    {
        return Task.FromResult<string?>("your-jwt-token");
    }
}

2. Create client options

using Pandax.SignalRClient;

public sealed class ChatClientOptions : SignalRClientOptions
{
}

Basic Usage

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Pandax.SignalRClient;

public interface IChatClient
{
    Task StartAsync(CancellationToken ct = default);
    Task SendMessageAsync(string user, string message);
    event Action<string, string>? OnMessageReceived;
}

public sealed class ChatClient : BaseSignalRClient<SignalRClientOptions>
{
    public event Action<string, string>? OnMessageReceived;

    public ChatClient(
        IAuthenticationProvider authenticationProvider,
        IOptions<SignalRClientOptions> options,
        ILogger<ChatClient> logger)
        : base(authenticationProvider, options, logger)
    {
    }

    protected override void OnRegisterHubEvents()
    {
        _connection.On<string, string>("ReceiveMessage", (user, message) =>
        {
            OnMessageReceived?.Invoke(user, message);
        });
    }

    public Task SendMessageAsync(string user, string message)
    {
        return _connection.InvokeAsync("SendMessage", user, message);
    }
}

4. Register the client with DI

using Microsoft.Extensions.DependencyInjection;

public static class ChatClientServiceCollectionExtensions
{
    public static IServiceCollection AddChatClient(
        this IServiceCollection services,
        Action<ChatClientOptions> configure)
    {
        services.Configure(configure);
        services.AddSingleton<IChatClient, ChatClient>();
        return services;
    }
}

5. Configure and run it

using Microsoft.Extensions.Hosting;
using Pandax.SignalRClient;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddHttpClient();
builder.Services.AddSingleton<IAuthenticationProvider, JwtAuthenticationProvider>();
builder.Services.AddChatClient(options =>
{
    options.Url = builder.Configuration["SignalR:HubUrl"]
        ?? throw new InvalidOperationException("Missing configuration: SignalR:HubUrl");
});

builder.Services.AddHostedService<ChatWorker>();

await builder.Build().RunAsync();

Example hosted service

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

public sealed class ChatWorker(IChatClient chatClient, ILogger<ChatWorker> logger)
    : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        chatClient.OnMessageReceived += (user, message) =>
        {
            logger.LogInformation("New message from {User}: {Message}", user, message);
        };

        await chatClient.StartAsync(stoppingToken);

        while (!stoppingToken.IsCancellationRequested)
        {
            await chatClient.SendMessageAsync("ConsoleApp", $"Heartbeat {DateTime.Now:O}");
            await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
        }
    }
}

Configuration

The client options support at least these settings:

  • Url: the SignalR hub endpoint
  • RetryPolicy: a custom IRetryPolicy implementation for reconnects

Example configuration:

{
  "SignalR": {
    "HubUrl": "https://localhost:5001/chatHub"
  }
}

Reconnect Behavior

By default, the library uses DefaultRetryPolicy, which retries up to 5 times with a 5 second delay.

You can replace it with your own implementation:

using Microsoft.AspNetCore.SignalR.Client;

public sealed class CustomRetryPolicy : IRetryPolicy
{
    public TimeSpan? NextRetryDelay(RetryContext retryContext)
    {
        return retryContext.PreviousRetryCount < 10
            ? TimeSpan.FromSeconds(3)
            : null;
    }
}

Then register it through options:

builder.Services.AddChatClient(options =>
{
    options.Url = builder.Configuration["SignalR:HubUrl"]!;
    options.RetryPolicy = new CustomRetryPolicy();
});

Sample Projects

  • SignalRClientSample: sample client application using the library
  • SignalRServerSample: sample SignalR server for local testing

Release Workflow

This repository includes a GitHub Actions workflow that publishes the NuGet package when a GitHub Release is published.

  • Only the SignalRClient library is packed and published as Pandax.SignalRClient
  • Pre-releases are skipped
  • Both .nupkg and .snupkg packages are pushed to NuGet

Repository

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
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.0.0 38 3/14/2026