Background on Sequence/Parallel semantics and exception aggregation is in Notification model.
Define the notification and its handlers
public sealed record OrderPlaced(Guid OrderId) : INotification;
public sealed class ReserveInventoryHandler : INotificationHandler<OrderPlaced>
{
public Task HandleAsync(OrderPlaced notification, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
}
public sealed class SendConfirmationEmailHandler : INotificationHandler<OrderPlaced>
{
public Task HandleAsync(OrderPlaced notification, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
}Register: manual
Call AddSendrNotification() once, then register every handler for a notification type in a
single AddNotificationHandler call, assigning each one to the Sequence and/or Parallel
group:
builder.Services
.AddSendrNotification()
.AddNotificationHandler<OrderPlaced>(x =>
{
x.Handler.Sequence.With<ReserveInventoryHandler>();
x.Handler.Parallel.With<SendConfirmationEmailHandler>();
});Publish
var publisher = serviceProvider.GetRequiredService<IPublisher>();
await publisher.PublishAsync(new OrderPlaced(order.Id), cancellationToken);PublishAsync accepts the non-generic INotification, so a polymorphic batch — for example,
events read back from an outbox — can be published without the caller knowing each concrete
notification type. A notification with no registered handlers is a no-op.
Decorating a specific handler entry
Each handler entry can carry its own decorator pipeline:
builder.Services
.AddSendrNotification()
.AddNotificationHandler<OrderPlaced>(x => x.Handler.Sequence
.With<ReserveInventoryHandler>(h => h.Decorator.With<LoggingNotificationDecorator>()));public sealed class LoggingNotificationDecorator(ILogger<LoggingNotificationDecorator> logger)
: INotificationDecorator
{
public async Task HandleAsync<TNotification>(
TNotification notification,
NotificationHandlerDelegate next,
CancellationToken cancellationToken = default)
where TNotification : INotification
{
logger.LogInformation("Handling {Notification}", typeof(TNotification).Name);
await next();
logger.LogInformation("Handled {Notification}", typeof(TNotification).Name);
}
}Register: generated
Davish.Sendr.Generators also discovers INotificationHandler<T> implementations. Multiple
classes may handle the same notification without triggering the ambiguous-handler diagnostic
(SENDR002) — there’s no per-handler execution-mode attribute; instead, RunAs sets how
every handler of the same notification runs, throughout the generated publisher. The
default is Sequence:
builder.Services.AddSendrNotification(o => o.UseGenerators());
// Or, to run same-notification handlers concurrently instead:
builder.Services.AddSendrNotification(o => o.UseGenerators(x => x.RunAs(NotificationRunMode.Parallel)));Neither mode guarantees relative order between handlers of the same notification. Decorators are declared the same way as elsewhere in generated registration:
[DecorateWith<LoggingNotificationDecorator>]
public sealed class ReserveInventoryHandler : INotificationHandler<OrderPlaced>
{
public Task HandleAsync(OrderPlaced notification, CancellationToken cancellationToken) { /* ... */ }
}Cross-assembly discovery works the same way as for AddSendr, with its own independent
assembly set:
o.UseGenerators(x => x.RunAs(NotificationRunMode.Parallel).IncludeAssemblyOf<ApplicationAssemblyMarker>());See Compile-time discovery and Enabling source generation for the full discovery model.
Manual
AddNotificationHandlerand generated registration are mutually exclusive for a givenIPublisher: onceUseGenerators()installs the generated publisher, it only dispatches to handlers discovered at compile time.