StackExchange.Redis.Extensions wraps Redis Pub/Sub with typed serialization and error handling.
await redis.SubscribeAsync<OrderEvent>("orders:new", async order =>
{
Console.WriteLine($"New order #{order.Id} for {order.Total:C}");
await ProcessOrderAsync(order);
});The handler receives a deserialized object. If the handler throws an exception, it is logged (not silently swallowed) and the subscription continues processing subsequent messages.
var subscriberCount = await redis.PublishAsync("orders:new", new OrderEvent
{
Id = 42,
Total = 99.99m,
});
Console.WriteLine($"Message sent to {subscriberCount} subscribers");// Unsubscribe a specific handler
await redis.UnsubscribeAsync<OrderEvent>("orders:new", myHandler);
// Unsubscribe all handlers on all channels
await redis.UnsubscribeAllAsync();When KeyPrefix is configured, it is automatically applied to both keys and Pub/Sub channels via StackExchange.Redis's ChannelPrefix mechanism:
// Config: KeyPrefix = "myapp:"
await redis.PublishAsync("orders", message);
// Actual Redis channel: "myapp:orders"You should NOT manually add the prefix to channel names.
Exceptions in subscription handlers are logged via ILogger and do not crash the process or drop the subscription:
sequenceDiagram
participant Redis
participant Handler as SubscribeAsync Handler
participant Logger as ILogger
Redis->>Handler: Message 1
Handler->>Handler: throw Exception
Handler->>Logger: LogError(exception, "Error processing...")
Redis->>Handler: Message 2
Handler->>Handler: Process successfully
Note: For the logger to work in production, ensure
RedisConfiguration.LoggerFactoryis set (it is automatically wired when usingAddStackExchangeRedisExtensionswith ASP.NET Core DI).