Getting Started
Architecture
NServiceBus
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Discarding expired messages

Component: MSMQ Transport
NuGet Package: NServiceBus.Transport.Msmq (2.x)
Target Version: NServiceBus 8.x

The MSMQ transport can handle messages with a Time-To-Be-Received (TTBR) set in two ways.

Native

When a message with a TTBR value is sent, NServiceBus translates the value to the native TTBR property of the MSMQ message. MSMQ continuously checks the Time-To-Be-Received of all queued messages. As soon as the message has expired, it is removed from the queue, and disk space gets reclaimed.

The MSMQ native TTBR implementation can be disabled for messages sent as part of a transaction. These messages rely on the non-native TTBR handling to ensure they are discarded when they are read by an endpoint, if the time to be received has expired. Messages sent outside of a transaction will still use the native TTBR capabilities built into the transport.

var transport = new MsmqTransport
{
    UseNonNativeTimeToBeReceivedInTransactions = true
};
endpointConfiguration.UseTransport(transport);

Non-native

NServiceBus also annotates outgoing messages with an NServiceBus.TimeToBeReceived header.

NServiceBus.Transport.Msmq version 1.0.x or below does not check the NServiceBus.TimeToBeReceived header.

NServiceBus.Transport.Msmq version 1.1 and above will consume a message without processing it if all of the following conditions are met:

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

The transport can be configured to ignore the NServiceBus.TimeToBeReceived header on incoming messages.

var transport = new MsmqTransport
{
    IgnoreIncomingTimeToBeReceivedHeaders = true
};
endpointConfiguration.UseTransport(transport);

Related Articles

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