Native message access

Component: Azure Service Bus Transport
NuGet Package NServiceBus.Transport.AzureServiceBus (1.x)
Target NServiceBus Version: 7.x

This document describes how to access native message information with Azure Service Bus transport version 1.4.0 and above.

Access to the native Azure Service Bus message details

It can sometimes be useful to access the native Service Bus message from behaviors and handlers. When a message is received, the transport adds the native Service Bus Message to the message processing context. Use the code below to access the message details from a pipeline behavior:

class DoNotAttemptMessageProcessingIfMessageIsNotLocked : Behavior<ITransportReceiveContext>
    public override Task Invoke(ITransportReceiveContext context, Func<Task> next)
        var lockedUntilUtc = context.Extensions.Get<Message>().SystemProperties.LockedUntilUtc;

        if (lockedUntilUtc <= DateTime.UtcNow)
            return next();


        return Task.CompletedTask;

The behavior above uses the native message's LockedUntilUtc system property to determine wherever the message has lost its lock as a result of aggressive prefetching and slow processing. The message with the lost lock will not go through recoverability, skipping attempted processing that otherwise would be guaranteed to fail due to the message's lost lock.

Last modified