azuremessage-queueazureservicebusazure-servicebus-queuesbrokeredmessage

How do I prevent duplicate message not to be inserted in Service Bus Queue while WebJob processing?


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);
        }

Solution

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