This sample demonstrates how to use the Windows Azure Service Bus messaging features within a transaction scope in order to ensure batches of messaging operations are committed atomically. See the Service Bus documentation for more information about the Service Bus before exploring the samples.

This sample demonstrates: sending and completing messages within a transaction scope; committing and aborting transactions.

Prerequisites

If you haven't already done so, please read the release notes document that explains how to sign up for a Windows Azure account and how to configure your environment.

Sample Flow

The sample flows in the following manner:

  1. Create a new queue on the Service Bus;
  2. Send and complete messages within a transaction scope:
    1. Send a plain text message to the newly created queue;
    2. Peek lock the message from the queue;
    3. Within a transaction scope, send a response message;
    4. Within a transaction scope, complete the initial message;
    5. Complete the transaction scope;
    6. Receive the response message.
  3. Send and complete messages within a transaction scope that rolls back:
    1. Send a plain text message to the queue;
    2. Peek lock the message from the queue;
    3. Within a transaction scope, send a response message;
    4. Within a transaction scope, complete the initial message;
    5. Abandon the transaction scope;
    6. Receive from the queue - since the transaction was not completed, the response message is not in the queue and the initial message is returned to the queue when its peek lock times out.
  4. Clean up resources associated with the sample.

Running the Sample

To run the sample:

  1. Build and run the sample in Visual Studio.
  2. When prompted, enter your Service Bus Namespace, Issuer Name and Issuer Key.

Expected Output

Service Namespace: <Your Namespace>
Issuer Name: owner
Issuer Secret: <Your Secret Key>

Creating Queues...

Scenario 1: Send/Complete in a Transaction and then Complete
Sending Message 'Message 1'
Peek-Lock the Message... Message 1
Inside Transaction 5378d67d-19f1-4d27-affa-3e93841be2aa:1
Sending Reply in a Transaction
Completing message in a Transaction
Marking the Transaction Scope as Completed
Receive the reply... Reply To - Message 1

Press [Enter] to move to the next scenario.


Scenario 2: Send/Complete in a Transaction and do not Complete
Sending Message 'Message 2'
Peek-Lock the Message... Message 2
Inside Transaction 5378d67d-19f1-4d27-affa-3e93841be2aa:2
Sending Reply in a Transaction
Completing message in a Transaction
Exiting the transaction scope without committing...
Receive the request again (this can take a while, because we're waiting for the
PeekLock to timeout)... Message 2

Press [Enter] to exit.
                    

Did you find this information useful? Please send your suggestions and comments about the documentation.