Skip to content
Shiny PlatformShiny Platform
Controls 3.0MAUI & Blazor NET11 Support

Contracts & Source Generator

A hub is described by one interface marked [UniversalHubContract]. Put it somewhere both sides can see it: a shared project, or the same app when a device can play either role.

using Shiny.UniversalHubs;
[UniversalHubContract]
public interface IGameHub
{
// client -> host
Task<JoinResult> Join(string playerName, string? avatarFile);
Task<MoveResult> MakeMove(int cell);
Task Rematch();
IAsyncEnumerable<int> Countdown(int from, CancellationToken cancellationToken);
// host -> clients
event Action<GameState> StateChanged;
event Action<string, string> Emote;
}
Return type Meaning
Task Completes once the hub method has run on the host
Task<T> Returns the hub method’s result
IAsyncEnumerable<T> Streams items as the hub yields them. Breaking out of the loop cancels the hub method
  • CancellationToken: a trailing CancellationToken parameter is never sent over the wire. Cancelling it on the client cancels the hub method on the host.
  • Parameters can be any type the hub’s serializer handles: primitives, records, arrays and so on.
  • Names are the wire identity. Methods are called by name, so names must be unique (no overloads), and renaming a method breaks clients that are already deployed. Add new methods rather than changing existing ones.

Events are fire-and-forget pushes from the host. They must be Action or Action<T1..T4>:

event Action Pinged;
event Action<GameState> StateChanged;
event Action<string, string> Emote;

On the client they are ordinary .NET events on the generated proxy. On the host the generator turns each one into a method you call on a push target: Clients.All.StateChanged(state).

Every argument, result and event value is serialized on its own with its static type, through an IHubSerializer. The default, JsonHubSerializer, uses Shiny’s AOT-friendly JSON, so every type that crosses the wire must be in a JsonSerializerContext, including primitives used as arguments:

[JsonSerializable(typeof(JoinResult))]
[JsonSerializable(typeof(MoveResult))]
[JsonSerializable(typeof(GameState))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(int))]
[JsonSourceGenerationOptions(UseStringEnumConverter = true)]
public partial class GameJsonContext : JsonSerializerContext;
// at startup, on both sides
Shiny.Json.AddContext(GameJsonContext.Default);

To use something else (MessagePack, protobuf…), implement IHubSerializer and either register it for the whole app (services.AddSingleton<IHubSerializer, MySerializer>() before AddUniversalServer / AddUniversalHubClient) or give it to one hub:

public interface IHubSerializer
{
string Name { get; } // "json", "msgpack" - travels in the handshake
byte[] Serialize<T>(T value);
T Deserialize<T>(ReadOnlySpan<byte> data);
}
server.AddHub<GameHub>("game", hub => hub.Serializer(new MessagePackHubSerializer()).UseHttp()); // host
services.AddUniversalHubClient<IGameHub>("game", o => o.Serializer(new MessagePackHubSerializer()).UseHttp()); // client
  • The serializer’s Name travels in the handshake. A hub refuses a client whose serializer has another name, so a mismatch fails at connect rather than on the first call. Two serializers with the same name must read each other.
  • Values travel exactly as the serializer wrote them, on every transport.
  • Dynamic hubs are JSON.

The generator ships inside the Shiny.UniversalHubs package, so there’s nothing to install.

Generated For Example
Client proxy every [UniversalHubContract] interface GameHubClient : HubProxy, IGameHub, IHubProxy<IGameHub>
Typed pushes every contract event Clients.All.StateChanged(state)
Hub dispatcher every class X : UniversalHub<TContract> maps method names to typed calls, with no reflection
IHubContext<X>.Clients every hub a C# 14 extension property: hubContext.Clients.Group("a").Emote(...)
Aspect Details
Proxy name the interface name without its leading I, plus Client. Change it with [UniversalHubContract(ProxyName = "MyClient")].
Contracts from other assemblies proxies are generated for contracts declared in a referenced assembly too. Those proxies are internal.
Projects in the same solution a project reference doesn’t carry the analyzer the way the package does. Reference Shiny.UniversalHubs as a package, or add the generator with OutputItemType="Analyzer".
Id Problem
SUH001 The hub doesn’t implement a contract method, or the signature doesn’t match. A trailing CancellationToken on the hub method is optional
SUH002 A method returns something other than Task, Task<T> or IAsyncEnumerable<T>
SUH003 Overloaded method names
SUH004 An event that isn’t Action / Action<…>, or has more than 4 arguments
SUH005 UniversalHub<T> where T isn’t an interface marked [UniversalHubContract]
SUH006 Generic methods, ref / out / in parameters, or more than 255 parameters