MSDN Library
İçindekiler tablosunu daralt
İçindekiler tablosunu genişlet
Bu içerik dilinizde bulunmamaktadır ancak İngilizce sürümüne buradan bakabilirsiniz.

How to Use the BrokeredMessage.Defer Method

Updated: August 25, 2015

When using the Service Bus BrokeredMessage class, you have the option of leaving a message in the queue using the Defer method. The deferred message can be processed later, at your discretion. Message deferral is useful for prioritizing messages, or for postponing a message that depends on some other process. After processing the message, you can call the Complete method to delete the message from the queue.

To use either the Defer or Complete method, set the queue client mode to ReceiveMode.PeekLock by calling the CreateQueueClient method of the MessagingFactory class.

When you defer the message, add the message SequenceNumber to a collection. To retrieve the message, call the Receive method of the QueueClient class. Pass the sequence number of the message as an argument to the Receive method.

To ensure that the message can be recalled, store the sequence numbers in a durable store. Otherwise, if the process fails, the record of the deferred messages will be lost.

To end the deferral, call the Complete method on each message, as needed.

For a sample app that demonstrates the Defer method, see Brokered Messaging: Deferred Messages.

  1. Add references to the following namespaces.

    using Microsoft.ServiceBus.Messaging;
    using Microsoft.ServiceBus;
  2. Create a MessagingFactory instance, and call the CreateQueueClient method. To create the MessagingFactory object, retrieve the Service Bus namespace URI, the Shared Access Signature (SAS) key name (default: RootManageSharedAccessKey), and the SAS key itself from the Azure management portal.

    private static QueueClient CreateQueueClient(string queueName) 
        // The serviceBusNamespace and serviceBusSASKey return the values specific to an Azure account. 
        string serviceBusSASKey = "yourServiceBusSASKey";
        string serviceBusNamespace = "yourServiceBusNamespace";
        string serviceBusKeyName = "RootManageSharedAccessKey";
        Uri runtimeUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceBusNamespace, string.Empty);
        MessagingFactory messagingFactory = MessagingFactory.Create(runtimeUri, 
            TokenProvider.CreateSharedAccessSignatureTokenProvider(serviceBusKeyName, serviceBusSASKey));
        return messagingFactory.CreateQueueClient(queueName,ReceiveMode.PeekLock); 

  1. Create a generic List of type long to store the numbers.

    List<long> deferredSequenceNumbers = new List<long>();
  2. Call the Receive method on the client. Check each message for deferral criteria, and call the Defer method as needed. Add the sequence number to the List collection.

    BrokeredMessage receivedMessage = queueClient.Receive(TimeSpan.FromSeconds(10));
        if (receivedMessage == null)
            // Low-priority messages will be dealt with later:
            if (receivedMessage.Properties["Priority"].ToString() == "Low")
               Console.WriteLine("Deferred message with id {0}.", receivedMessage.MessageId);
               // Deferred messages can only be retrieved by message receipt. Here, keeping track of the
               // message receipt for a later retrieval: 

  1. Iterate through the collection of sequence numbers using the Receive method.

    foreach (long sequenceNumber in deferredSequenceNumbers)
  2. The ProcessMessage method calls the Complete method on each message.

    private static void ProcessMessage(BrokeredMessage message)
        // To do: code to process the message.

For a sample application that demonstrates the Defer method, see Brokered Messaging: Deferred Messages,

See Also

© 2015 Microsoft