title: How do I prevent duplicate message not to be inserted in Service Bus Queue while WebJob processing?
body: <p>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 <code>webjob</code> is processing first message.</p>
  <p>I tried following code:</p>
  <pre><code> var namespaceManager =
                  NamespaceManager.CreateFromConnectionString(connectionString);
  
              if (!namespaceManager.QueueExists(queueName))
              {
                  namespaceManager.CreateQueue(new QueueDescription(queueName) { RequiresDuplicateDetection = true });
              }
  </code></pre>
  <p>property <code>RequiresDuplicateDetection</code> should ensure about duplicate message.</p>
  <pre><code> // Get messageFactory for runtime operation
              MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
  
              QueueClient queueClient = messagingFactory.CreateQueueClient(&quot;TestQueue&quot;);
  
              BrokeredMessage message = new BrokeredMessage();
              message.MessageId = &quot;Localization&quot;;
              queueClient.Send(message);
  </code></pre>
  <p>But <code>webjob</code> 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.</p>
  <p>I tried <a href="https://code.msdn.microsoft.com/windowsazure/Brokered-Messaging-c0acea25" rel="nofollow noreferrer">MSDN</a> but it is not working in Azure Webjob.</p>
  <p>WebJob code:</p>
  <pre><code>public static void ProcessQueueMessage([ServiceBusTrigger(&quot;TestQueue&quot;)] BrokeredMessage message, TextWriter log)
          {
   log.WriteLine(&quot;Webjob Start&quot; + message.MessageId + DateTime.Now);
              Thread.Sleep(150000);
              log.WriteLine(&quot;Webjob End&quot; + message.MessageId + DateTime.Now);
          }
  </code></pre>