Is it possible to skip a step in the pipeline conditionally? I would like to skip the DataBusSendBehavior
based on a value set in the ContextBag
.
To give some more insights why I want to do this: I’m implementing a SendOnly outbox for SQL Server and use a behavior on the IOutgoingLogicalMessageContext
for this. This means a message passes the pipeline twice: first time to store it in the outbox, second time to actually dispatch it once the transaction is completed. Second time it passes the pipeline, the databus property is already replaced so I don’t want it to execute the DataBusSendBehavior.
To give an idea of how the SendOnly outbox behavior works:
internal class SendOnlyOutboxBehavior : Behavior<IOutgoingLogicalMessageContext>
{
public override async Task Invoke(IOutgoingLogicalMessageContext context, Func<Task> next)
{
if (context.Extensions.ShouldSkipSendOnlyOutbox())
{
await next();
}
else
{
var messageId = new Guid(context.MessageId);
var messageType = context.Message.MessageType.AssemblyQualifiedName;
var message = context.Message.Instance;
var deliverAt = GetDeliverAt(context);
var headers = GetHeaders(context);
var outboxMessage = new SendOnlyOutboxMessage(messageId, messageType, message, deliverAt, headers);
await _sendOnlyOutboxStorageService.AddAsync(outboxMessage);
}
}
}