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

Training Courses

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

Receiving Message Sessions

When receiving messages that are part of a session, a MessageSession is accepted on the QueueClient instead of using the QueueClient directly. The AcceptMessageSession method on the QueueClient class will wait until the first message in a session is detected on the queue. Once this happens the MessageSession is initialized with the SessionId of that message and the messages from that session can be received. Accepting a message session can result in a timeout exception if no message sessions are available within the default operation timeout for the queue client, the default setting for this is 60 seconds.

The changes made to the checkout console application are shown below.

 

while (true)

{

    int receivedCount = 0;

    double billTotal = 0.0;

 

    // Accept a message session and handle any timeout exceptions.

    MessageSession orderSession = null;

    while (orderSession == null)

    {

        try

        {

            Console.ForegroundColor = ConsoleColor.Magenta;

            Console.WriteLine("Accepting message session...");

 

            // Accept the message session

            orderSession = queueClient.AcceptMessageSession();

 

            Console.WriteLine("Session accepted.");

        }

        catch (TimeoutException ex)

        {

            Console.WriteLine(ex.Message);

        }

    }

               

    Console.ForegroundColor = ConsoleColor.Cyan;

    Console.WriteLine("Processing order");

    Console.WriteLine(orderSession.SessionId);

    Console.ForegroundColor = ConsoleColor.Cyan;

 

    // Receive the order batch tag read messages.              

    while (true)

    {

        //BrokeredMessage receivedTagRead = queueClient.Receive(TimeSpan.FromSeconds(5));

 

        // Receive the tag read messages from the message session.

        BrokeredMessage receivedTagRead = orderSession.Receive(TimeSpan.FromSeconds(5));

 

        if (receivedTagRead != null)

        {

            RfidTag tag = receivedTagRead.GetBody<RfidTag>();

            Console.WriteLine("Bill for {0}", tag.Product);

            receivedCount++;

            billTotal += tag.Price;

 

            // Mark the message as complete

            receivedTagRead.Complete();

        }

        else

        {

            break;

        }

    }

 

    if (receivedCount > 0)

    {

        // Bill the customer.

        Console.ForegroundColor = ConsoleColor.Green;

        Console.WriteLine

            ("Bill customer £{0} for {1} items.", billTotal, receivedCount);

        Console.WriteLine();

        Console.ResetColor();

    }

}

 

 

 

When the checkout console application is accepting a message session the value for the SessionId is not known. Accepting a session without specifying a SessionId will result in the first message on the queue initializing the session with the SessionId of that message.

Once these changes have been made the solution should send all the RFID tags for each order in a session with a unique SessionId, and receive and process the customer bills using those sessions.

Speaking Engagements