0
votes

The below code reads the messages Asynchronously BUT I want to read one message at one time and the next message in the queue next day or after 1 week kind of use case. How can I read only one message Synchronously and keep the rest of the messages in the azure service bus queue for next time read.

enter code here

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

namespace ConsoleApp2
  {
class Program
{
    const string ServiceBusConnectionString = "xxxxxx";
    const string QueueName = "xxx";
    static IQueueClient queueClient;
    public static async Task Main(string[] args)
    {
        queueClient = new QueueClient(ServiceBusConnectionString, QueueName);

        RegisterOnMessageHandlerAndReceiveMessages();

        Console.ReadKey();

        await queueClient.CloseAsync();
    }
    static void RegisterOnMessageHandlerAndReceiveMessages()
    {
        var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
        {
            MaxConcurrentCalls = 10,
            AutoComplete = false
        };
        queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
    }
    static async Task ProcessMessagesAsync(Message message, CancellationToken token)
    {
        Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
        await queueClient.CompleteAsync(message.SystemProperties.LockToken);
    }
    static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
    {
        Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
        var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
        Console.WriteLine("Exception context for troubleshooting:");
        Console.WriteLine($"- Endpoint: {context.Endpoint}");
        Console.WriteLine($"- Entity Path: {context.EntityPath}");
        Console.WriteLine($"- Executing Action: {context.Action}");
        return Task.CompletedTask;
    }
}

}

1
Instead of registering the messagehandler, can you just use the synchronous QueueClient.Receive() method? - Frank Alvaro
@FrankAlvaro I couldn't find this method in Microsoft.Azure.ServiceBus namespace. I couldn't figure out how to create MessagingFactory and read the messages. Do you have sample reference ? - SNew
Apologies - the QueueClient.Receive method is in the Microsoft.ServiceBus.Messaging namespace. There is a simple walkthrough for simple brokered messaging here, which appears to use a synchronous approach. - Frank Alvaro
@FrankAlvaro Microsoft.ServiceBus.Messaging is deprecated and not working also. Do you have any ideas with the latest Microsoft.Azure.ServiceBus namespace - SNew
@SNew Did you get a resolution for controlling the messages. - Darey

1 Answers

1
votes

You should be able to achieve this by using MessageReceiver class.

Take a look at this example for the implementation details:

https://github.com/Azure/azure-service-bus/blob/master/samples/DotNet/Microsoft.ServiceBus.Messaging/ReceiveLoop/Program.cs