1

I want to be sure that if same message is already present in the queue then second message should be ignored (not inserted to queue) while webjob is processing first message.

I tried following code:

 var namespaceManager =
                NamespaceManager.CreateFromConnectionString(connectionString);

            if (!namespaceManager.QueueExists(queueName))
            {
                namespaceManager.CreateQueue(new QueueDescription(queueName) { RequiresDuplicateDetection = true });
            }

property RequiresDuplicateDetection should ensure about duplicate message.

 // Get messageFactory for runtime operation
            MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);

            QueueClient queueClient = messagingFactory.CreateQueueClient("TestQueue");

            BrokeredMessage message = new BrokeredMessage();
            message.MessageId = "Localization";
            queueClient.Send(message);

But webjob gets trigger for every messageId. I gave sleep time 150000 milliseconds but before that I tried to insert same message to same queue, which should not be inserted because of duplicate message.

I tried MSDN but it is not working in Azure Webjob.

WebJob code:

public static void ProcessQueueMessage([ServiceBusTrigger("TestQueue")] BrokeredMessage message, TextWriter log)
        {
 log.WriteLine("Webjob Start" + message.MessageId + DateTime.Now);
            Thread.Sleep(150000);
            log.WriteLine("Webjob End" + message.MessageId + DateTime.Now);
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
Neo
  • 15,491
  • 59
  • 215
  • 405

1 Answers1

3

Duplicate message detection is based on the MessageId of the BrokeredMessage. The Azure Product Team has a sample illustrating this feature here.

Jeroen Maes
  • 673
  • 3
  • 8
  • This does unfortunately not work :( Duplicate detection checks if SB received a message of the same ID in the last x seconds/minutes and does not check if the message exists in the queue or not. – GETah Dec 01 '20 at 11:26
  • Correct: "Enabling duplicate detection helps keep track of the application-controlled MessageId of all messages sent into a queue or topic during a specified time window." https://learn.microsoft.com/en-us/azure/service-bus-messaging/duplicate-detection – Jeroen Maes Dec 02 '20 at 14:25