Consumer Behaviors Pipeline
Each message consumed by Silverback is processed by a pipeline of behaviors that enable features and cross-cutting concerns. The SortIndex property of each behavior determines the order in which they are executed and each behavior must call the next behavior in the pipeline to continue processing.
Default Behaviors
The following table lists the default behaviors that are executed when consuming a message.
| Name | Index | Description |
|---|---|---|
| ActivityConsumerBehavior | 100 | Starts an Activity for the incoming message and extracts tracing information from headers when available. |
| FatalExceptionLoggerConsumerBehavior | 200 | Logs fatal exceptions thrown while processing the message (used to flag unrecoverable errors and improve observability). |
| CustomHeadersMapperConsumerBehavior | 300 | Maps message headers to the destination names configured with the custom header mappings. |
| TransactionHandlerConsumerBehavior | 400 | Manages the transaction scope for the message processing, using the configured IConsumerTransactionManager implementations. |
| RawSequencerConsumerBehavior | 500 | Handles raw sequence-related headers (for messages published as sequences) and prepares the message for sequencing logic. |
| DecryptorConsumerBehavior | 700 | Decrypts the message payload when encryption is enabled on the endpoint. |
| BinaryMessageHandlerConsumerBehavior | 800 | Switches to the BinaryMessageSerializer when the consumed message implements IBinaryMessage and handles binary payloads. |
| DeserializerConsumerBehavior | 900 | Deserializes the message payload into the target CLR type using the configured IMessageDeserializer. |
| ValidatorConsumerBehavior | 950 | Validates the deserialized message using the configured validation rules and validators. |
| HeadersReaderConsumerBehavior | 1000 | Reads headers and maps them to message properties decorated with the HeaderAttribute. |
| SequencerConsumerBehavior | 1100 | Uses the available implementations of ISequenceReader to reassemble sequences (e.g., chunked messages) and pass the resulting message(s) down the pipeline. |
| PublisherConsumerBehavior | 2000 | Publishes the message to the configured message handlers, in-memory subscribers or any other final destinations. |
Custom Behaviors
It is possible to add custom behaviors to the consumer pipeline to implement cross-cutting concerns or extend Silverback's processing. A consumer behavior is a class implementing the IConsumerBehavior interface. The HandleAsync method is invoked for each consumed message, allowing the behavior to modify the envelope, perform side-effects, or short-circuit the pipeline.
public class CustomConsumerBehavior : IConsumerBehavior
{
public int SortIndex => 300;
public async ValueTask HandleAsync(
ConsumerPipelineContext context,
ConsumerBehaviorHandler next,
CancellationToken cancellationToken)
{
// ...
await next(context, cancellationToken);
}
}
The custom behavior must be registered at startup using the AddSingletonBrokerBehavior, or AddTransientBrokerBehavior method. The transient behavior will be resolved once per consumer, while the singleton behavior will be shared.
services
.AddSilverback()
...
.AddSingletonBrokerBehavior<CustomConsumerBehavior>();
Additional services can be injected into the behavior constructor or resolved from the ServiceProvider available on the ConsumerPipelineContext when a scoped instance is required.