Skip Navigation LinksAzure Developers Guide > Azure Service Bus > Brokered Messaging > Brokered Messaging Features & Patterns > Message Sessions Example

Training Courses

All course material is in English, and courses are delivered in English. Feel free to contact me for further information.

Message Sessions Example

Service Bus queues and subscriptions provide support for requiring sessions when receiving messages. Sessions are useful in a number of scenarios where a group of messages are to be grouped together, such as all the items in an order, or when a large document has been split into smaller messages. Sessions are especially useful when an application has load balanced receivers as they ensure that all messages in the same session are received by the same instance of the receiving application.

In order to work with sessions the queue or subscription must be created with the RequiresSession property set to true. Messages sent to the queues and topics that contain session aware subscriptions must have a value set for their SessionId properties. When receiving messages from session aware queues and subscriptions a MessageSession must be used instead of a QueueClient or SubscriptionClient.

The example used in this scenario will be based on the RFID processing solution that was used in the duplicate messaging scenario. The scenario was based on an RFID tag reader that would read the RFID tags for products in an order and send them, via a queue, to a check out application. The scenario worked well in that duplicate detection was used to suppress any duplicate RFID tag messages.

There is, however, a major flaw in the implementation. If the RFID tags for more than one order are read at the same time the checkout application is unable to determine which tag reads came from which order, and will be merged onto a single order. It is also not possible to load-balance the checkout applications, as items from the different orders will be received by different instances of the application.

This can be seen if the application is tested with three tag reader consoles sending orders to checkout console.


When all three tag reader consoles send orders simultaneously the following results are displayed in the checkout console.



The tag-read messages sent by the three console applications have all been aggregated into one bill for the customer.

In order for the solution to ensure that the tag-read messages from a single are processed as one batch or sequence rather than individual messages, the solution must be modified to use message sessions.

Speaking Engagements