Possible to conditionally skip a step in the pipeline?

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);
        }
    }
}

Hi

Unfortunately there is no way to skip a behavior. Alternatively what you could do is take a dependency on IDispatchMessages in your code to avoid going through the pipeline the second time. So first time around you go through the pipeline and capture the messages just before being passed to the dispatcher.

Then when you dispatch outgoing messages you go straight to the dispatcher.

Szymon

Thank you for your quick reply.

I also read about the NServiceBus.TransactionalSession package which is under development. If I understand correctly, this would be a solution for this use case as well? Can you share any information on a potential release date?

Jeroen

Hi

Yes. The work on that package is fairly advanced. We plan to release it both for V7 and V8 of NServiceBus. The V7 release is going to happen in the next couple of weeks. We don’t yet have the release date of V8.

The API of the NServiceBus.TransactionalSession is going to be as close to the regular outbox as possible. The difference is that the session for which the atomic guarantees are provided will be opened outside of a handler.

I’ll let you know as soon as we have a package and documentation available.

Szymon

Sounds good. Thanks.

Jeroen

Hi @jeroenj

We have released the transactional session.

Regards,
Daniel

1 Like