0

I have a requirement to process the same set of messages together and for this, I was trying Azure Service Bus Sessions Enabled feature. To test this, I created a very simple application, a message is submitted successfully in a queue, however, while trying to receive the message in "ReceiveSessionMessage" function, a message session is not returned and the program exits after this line.

I am not able to figure out the exact root cause, any help would be much appreciated. Thanks

[var messageSession = await sessionClient.AcceptMessageSessionAsync();]

Program

using Microsoft.Azure.ServiceBus;
using System;
using System.Text;
using System.Threading.Tasks;

namespace TestSendReceiveMessagesAzure
{
    class Program
    {
        static string connectionString = "";
        static string queueName = "demosessionqueue";

        static void Main(string[] args)
        {
            Console.WriteLine("Test Service Bus Session! enable feature");
            SendMessage();
            Console.WriteLine("Message Pushed");
            ReceiveSessionMessage();
        }

        private static void SendMessage()
        {
            QueueClient queueClilent = new QueueClient(connectionString, queueName, ReceiveMode.PeekLock);
            string msgJson = "{PizzaType:Veggie,SessionID:SessionId0101}";
            Message message = new Message(Encoding.UTF8.GetBytes(msgJson))
            {
                SessionId = "SessionId0101"
            };
            Console.WriteLine(msgJson);
            queueClilent.SendAsync(message).Wait();
        }

        private static async Task ReceiveSessionMessage()
        {
            var sessionClient = new SessionClient(connectionString, queueName, ReceiveMode.PeekLock);
            Console.WriteLine("Accepting a message session...");
            try
            {
                var messageSession = await sessionClient.AcceptMessageSessionAsync();
                Console.WriteLine($"Message.SessionID={messageSession.SessionId}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }
    }
}

Console Output

enter image description here

fortanu82
  • 351
  • 1
  • 9
  • 19
  • Please see the sample code here: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample03_SendReceiveSessions.md. – Gaurav Mantri Jul 02 '21 at 15:18
  • Thanks! @GauravMantri for your response. I tried the one which you shared and called it from Program and it resulted in the same issue. Later I found its an issue with the concepts of async and await, which I have explained in the Answer. – fortanu82 Jul 05 '21 at 08:53

1 Answers1

0

The issue is with the declaration of

static void Main(string[] args) , and the calling method "ReceiveSessionMessage()" in it. The correct way of calling this function from the Program.cs was

  static async Task Main(string[] args)
    {

        Console.WriteLine("Message Session Handler..");
        await MessageSessionReceiver();
    }

"ReceiveSessionMessage" function was an async function and the calling function did not have the await keyword mentioned due to which the program exited. After changing the syntax to add await, it worked.

fortanu82
  • 351
  • 1
  • 9
  • 19