AI agents: use the documentation index at llms.txt to locate machine-readable pages. This section is indexed by https://docs.particular.net/transports/llms.txt. The markdown version of this page is served as plain text. An MCP server at /mcp serves the same content via the search_docs and read_doc tools; it is read-only and needs no credentials. Markdown versions of documentation pages are available by appending .md to the page URL. Directory URLs use index.md. They are served as text/plain because some retrieval backends reject text/markdown.

Ordered delivery with sessions

Target Version:
NServiceBus 10.x

Ordered delivery

Azure Service Bus supports ordered delivery through message sessions. Messages that share a session ID are delivered in the order they were sent.

To process messages in order, enable sessions on the transport:

transport.EnableSessions = true;

Sessions apply to the whole endpoint. When installers are enabled, the transport creates the endpoint's input queue, satellite queues, and subscriptions with sessions required. If installers are not enabled, use the CLI tooling with the --sessions option to create the endpoint's input queue, topics, and subscriptions with sessions required.

Azure Service Bus can't enable sessions on an existing queue or subscription, and installers don't update entities that already exist. If sessions are enabled on an endpoint whose queue doesn't require sessions, the endpoint can't receive its messages and logs an error that the entity isn't session-enabled.

Setting the session ID

Every message sent to a session-enabled queue or published to a session-enabled subscription must have a session ID. Sending a message without a session ID to a session-enabled queue throws an exception. A session-enabled queue or subscription can therefore only process messages that have a session ID.

Messages that should be processed in order must share the same session ID. Choose a session ID that identifies the context where order matters, such as an order ID.

To send a message to a session-enabled queue:

var sendOptions = new SendOptions();
sendOptions.SetSessionId("my-session-id");

To publish a message to a topic with session-enabled subscriptions:

var publishOptions = new PublishOptions();
publishOptions.SetSessionId("my-session-id");

To reply to a message to a session-enabled queue:

var replyOptions = new ReplyOptions();
replyOptions.SetSessionId("my-session-id");

The session ID is set for each message, including messages sent or published from a handler. Only saga timeouts automatically get the session ID of the message that requested them.

Messages with a session ID that a handler sends or publishes are dispatched in the order in which the handler sent or published them.

Processing

The transport processes one message per session at a time on each endpoint instance. Messages from different sessions are processed concurrently, up to the endpoint's concurrency limit.

Session-enabled endpoints don't support TransportTransactionMode.None. The endpoint fails to start when it's configured.

Failures

Immediate retries are supported and don't break the processing order.

Delayed retries would break the processing order, so session-enabled endpoints don't support them. The endpoint fails to start unless delayed retries are disabled.

This applies only to delayed retries. Session-enabled endpoints support delayed delivery, including saga timeouts.

When immediate retries are exhausted, the message is moved to the error queue, and the next message in the session is processed. This breaks the processing order. Retrying a message from the error queue or the dead-letter queue doesn't restore its original position in the session.

Publish/subscribe

A session-enabled endpoint receives events through session-enabled subscriptions. The transport forwards events from each subscription to the endpoint's input queue, for the events that the endpoint subscribes to, through automatic subscriptions or an explicit subscribe call. Events in a subscription that the endpoint doesn't subscribe to aren't forwarded.

Creating subscriptions requires Manage rights. See access rights for the requirements.

Forwarding a large backlog, for example after the endpoint has been stopped, can exceed the namespace's throughput limits, and Azure Service Bus can throttle it.

Session state

Session state stores data with the session on the broker. Use it to share state between the messages of a session, for example to build lightweight workflows.

Get the session state from the handler context using GetSessionState(), read it with Get(), and write it with Set():

var sessionState = context.GetSessionState();
var orderState = await sessionState.Get<OrderState>(context.CancellationToken) ?? new OrderState();
orderState.ItemCount += 1;
await sessionState.Set(orderState, context.CancellationToken);

In SendsAtomicWithReceive mode, session state changes are committed together with the incoming message. In ReceiveOnly mode, which the outbox requires, they are written immediately and aren't rolled back when processing fails. The outbox doesn't cover session state, so make session state changes idempotent when the message can be processed more than once.

Session state stays on the broker after all messages in the session have been processed, and it counts toward the queue's storage quota. Remove it with Clear() when the session is complete:

var sessionState = context.GetSessionState();
await sessionState.Clear(context.CancellationToken);

Sagas

When a saga handles messages from a session-enabled queue, correlate it on the same value that's used as the session ID. All messages for a saga instance are then processed in order.

Endpoint setup and licensing

If a logical endpoint needs to process some message types in order while allowing others to be processed without ordering, it should be split into two endpoints: one with sessions enabled and the other with sessions disabled.

The two endpoints can also be hosted in one process.

These endpoints should have distinct names, such as Sales.Ordered and Sales.Unordered. However, they will be treated as a single logical endpoint for licensing purposes and will count as one licensed endpoint.

List of Samples

Related Articles