Getting Started
Architecture
NServiceBus
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Modernization
Samples

Discarding expired messages

Component: MSMQ Transport
NuGet Package: NServiceBus.Transport.Msmq (1.x - 1.1)
Target Version: NServiceBus 7.x
Standard support for version 7.x of NServiceBus has expired. For more information see our Support Policy.

The MSMQ transport can discard messages that exceed a configured Time-To-Be-Received (TTBR) in two ways: natively or through non-native handling by NServiceBus.

Native

When a message with a TTBR value is sent, NServiceBus maps that value to the native TimeToBeReceived property of the MSMQ message. MSMQ continuously checks the TTBR of queued messages. Once a message has expired, it is automatically removed from the queue and the corresponding disk space is reclaimed.

The MSMQ native TTBR implementation can be disabled for messages sent as part of a transaction. In this case, messages rely on non-native TTBR handling to ensure they are discarded at receive time if the Time-To-Be-Received has expired.

Messages sent outside of a transaction will still use MSMQ's built-in native TTBR functionality.

var transport = endpointConfiguration.UseTransport<MsmqTransport>();
transport.DisableNativeTimeToBeReceivedInTransactions();

Non-native

In addition to using MSMQ's native behavior, NServiceBus also includes a NServiceBus.TimeToBeReceived header on outgoing messages.

NServiceBus.Transport.Msmq version 1.0.x and earlier does not evaluate the NServiceBus.TimeToBeReceived header on incoming messages.

Starting with version 1.1, the transport will discard a message without processing it if all of the following conditions are met:

  • The message contains an NServiceBus.TimeSent header.
  • The message contains an NServiceBus.TimeToBeReceived header.
  • The calculated cut-off time (Time Sent + Time To Be Received) has already passed.

If necessary, the transport can be configured to ignore the NServiceBus.TimeToBeReceived header on incoming messages:

var transport = endpointConfiguration.UseTransport<MsmqTransport>();
transport.IgnoreIncomingTimeToBeReceivedHeaders();

Related Articles

  • Discarding Old Messages
    Automatically discard messages if they have not been processed within a given period of time.