This sample demonstrates how to use the Windows Azure Service Bus and the Request/Response functionality.
The sample shows simple clients and servers communicating via a Service Bus queue. The SampleManager first prompts for service namespace credentials. These are used to authenticate with the Access Control service, and acquire an access token that proves to the Service Bus infrastructure that the client is authorized to access the queue. The SampleManager creates two queues, one for requests which does not require sessions and one for responses which does require sessions. It then creates four clients and a server. Each client sends messages on the request queue and then waits for a message on the response queue. The server reads a message from the request queue, then sends a message on the response queue.
Many application scenarios involve two-way communications in which a sender would like to receive and correlate responses for the messages that it sends. This is supported in Service Bus through the use of the properties SessionId and ReplyToSessionId on a message. Consider a pattern involving several clients that are sending messages and expect responses for these messages to be directed back to them. To enable this pattern, a given client, say client “ABC”, would set ReplyToSessionId=”ABC” on any messages it sends and also use SessionReceiver on a reply topic/subscription or queue to listen for messages where SessionId=ABC. To complete the pattern, any processor of the message would set SessionId=ReplyToSessionId once a message has been processed.
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.
SampleManager
The SampleManager's flow:
- Gets the user credentials and creates a NamespaceManager (namespaceClient). This entity holds the credentials and is used for all messaging management operations.
- The namespaceClient is used to create queues for communication between the clients and server.
- SampleManager then creates and starts the clients and server processes, passing the user credentials to each. Clients are numbered 0-3, and use those numbers as their response session id.
- On user input, the SampleManager closes the clients and server, and deletes the queues.
Note: The static ServiceBusEnvironment.CreateServiceUri
function is provided to help construct the URI with the correct format and
domain name. It is strongly recommended that you use this function instead of
building the URI from scratch because the URI construction logic and format
might change in future releases. |
Client
The Client's flow:
- Gets user credentials and client # as parameters from SampleManager and creates a MessagingFactory (messagingFactory). This entity holds the credentials and is used for all messaging runtime operations.
- Opens QueueClients to the request and response queues, using the messagingFactory.
- Sends messages to the request queue.
- Receives messages for its associated session (the client # passed in from the SampleManager).
- Closes QueueClients.
Server
The Server's flow:
- Gets user credentials as parameters from SampleManager and creates a MessagingFactory (messagingFactory).
- Opens QueueClients to the request and response queues, using the messagingFactory.
- Receives messages on the request queue.
- Sends messages on the response queue, setting the session to the client # so that the correct client will receive the response.
- Closes QueueClients.
Running the Sample
To run the sample, build the solution in Visual Studio or from the command line, then run the resulting SampleManager executable file. The program will prompt for your service namespace and the issuer credentials. For the issuer secret, be sure to use the "Default Issuer Key" value (typically "owner") from the portal, rather than one of the management keys.
Expected Output - SampleManager
Please provide the namespace to use: <service namespace> Please provide the Issuer name to use: <issuer name> Please provide the Issuer key to use: <issuer key> Creating Queues... Created RequestQueue, Queue.RequiresSession = false Created ResponseQueue, Queue.RequiresSession = true Launching clients and servers... Press [Enter] to exit. |
Expected Output - Server
Ready to receive messages from RequestQueue... Reading messages from queue RequestQueue REQUEST: 0 - Client 2. REQUEST: 0 - Client 0. REQUEST: 0 - Client 1. REQUEST: 0 - Client 3. REQUEST: 1 - Client 2. REQUEST: 1 - Client 0. REQUEST: 1 - Client 1. REQUEST: 1 - Client 3. REQUEST: 2 - Client 2. REQUEST: 2 - Client 0. REQUEST: 2 - Client 1. REQUEST: 2 - Client 3. ... REQUEST: 9 - Client 2. REQUEST: 9 - Client 0. REQUEST: 9 - Client 1. REQUEST: 9 - Client 3. Server complete. |
Expected Output - Client
Preparing to send request messages to RequestQueue... Sending request messages to queue RequestQueue Receiving response messages on queue ResponseQueue REQUEST: 0 - Client 0. RESPONSE: 0 - Client 0. REQUEST: 1 - Client 0. RESPONSE: 1 - Client 0. REQUEST: 2 - Client 0. RESPONSE: 2 - Client 0. ... REQUEST: 9 - Client 0. RESPONSE: 9 - Client 0. Client complete. |