Table of Contents

Getting started

Tharga.Communication ships a server and a client that talk over a SignalR hub. The server is a regular ASP.NET Core host; the client is any .NET host (console, worker, Blazor, etc.).

Install

dotnet add package Tharga.Communication

For MCP introspection (connected clients, subscriptions, handlers) also add:

dotnet add package Tharga.Communication.Mcp

Server

var builder = WebApplication.CreateBuilder(args);

builder.AddThargaCommunicationServer(options =>
{
    options.RegisterClientStateService<MyClientStateService>();
    options.RegisterClientRepository<MemoryClientRepository<ClientConnectionInfo>, ClientConnectionInfo>();
});

var app = builder.Build();
app.UseThargaCommunicationServer();
app.Run();

AddThargaCommunicationServer wires the SignalR hub, message handler dispatch, and the default API key validator. UseThargaCommunicationServer maps the hub endpoint (default /hub).

You must provide:

  • A client state service (subclass ClientStateServiceBase<T>) — tracks connected clients
  • A client repository (use MemoryClientRepository<T> or your own backing store)

Client

In appsettings.json:

{
  "Tharga": {
    "Communication": {
      "ServerAddress": "https://localhost:5001"
    }
  }
}
var builder = Host.CreateApplicationBuilder(args);
builder.AddThargaCommunicationClient();
builder.Build().Run();

Every client option except AdditionalAssemblies can be set in this section, including ApiKey, ClientType, ClientMachine, SendMessageTimeout and ReconnectDelays. Values can also be supplied via the options callback, which runs after the section is read and overrides it:

builder.AddThargaCommunicationClient(o =>
{
    o.ServerAddress = "https://localhost:5001";
    o.ApiKey = "my-secret-key";
});

A host without an IHostApplicationBuilder, and a library that registers its own services, can register on the IServiceCollection. The configuration is optional, and no service provider is built during registration:

services.AddThargaCommunicationClient(configuration, o => o.ApiKey = "my-secret-key");

To hold more than one connection, register a named client. It has its own options, connection, reconnect state, subscription state and handlers, and binds the Tharga:Communication:<name> section. A named client scans only the Tharga.Communication assembly and its AdditionalAssemblies for handlers:

services.AddThargaCommunicationClient("quilt4net", o =>
{
    o.ServerAddress = "https://quilt4net.example";
    o.AdditionalAssemblies = [typeof(SomeLibraryHandler).Assembly];
});

public MyService([FromKeyedServices("quilt4net")] IClientCommunication client) { }

The client maintains a persistent SignalR connection with automatic reconnection.

Next steps