This sample demonstrates how to use various Windows Azure Service Bus Messaging Features from Silverlight through the REST Service Bus endpoint. The sample comes in two pieces. The Microsoft.Samples.ServiceBus.Messaging library includes a complete REST API for accessing Service Bus Messaging features from Silverlight; the SilverlightChat application is built using that library and shows how to build a Silverlight application which interacts with Service Bus messaging features and follows the MVVM pattern. See the Service Bus documentation for more information about the Service Bus before exploring the samples.

This sample demonstrates:

  1. Retrieving an authorization token from the Access Control Service, determining its expiry time and renewing the token when necessary;
  2. Retrieving a list of topics within a service namespace and determining if a topic contains a named subscription;
  3. Adding topics to the service namespace and detecting conflicts with existing topics;
  4. Adding a subscription to a topic and receiving messages from that subscription;
  5. Receiving messages with PeekLock semantics including Completing messages when they are processed successfully and abandoning them otherwise;
  6. Sending messages to a topic and using Message Properties from the REST endpoint;
  7. Deleting topics and subscriptions

In this sample, many participants can chat in multiple chat rooms by using the Service Bus. When the application starts, each user enters a unique username and clicks login. Then, a list of available 'Chat Rooms' (Topics) is downloaded. Users can subscribe to various rooms to receive updates from them even when they are not present in that room, they may send and receive messages in any room they are subscribed to.

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.

Before You Run the Sample

To compile and run the Silverlight Sample, you first need to have the Silverlight 4 Tools for Visual Studio 2010 installed on your computer.

To run the Windows Phone sample you first need to install the Windows Phone Developer Tools.

In Silverlight, cross domain networking is restricted; any services you wish to call from a Silverlight application (including the Windows Azure Service Bus) are responsible for publishing a Client Access Policy file - you can learn more about Network Security Access Restrictions in Silverlight on MSDN. This sample access network resources in two realms: firstly, an HTTP POST request is made to the Access Control Service; next HTTP POST, PUT and DELETE requests are made to the Service Bus. The Access Control Service publishes a default client access policy document which permits cross domain execution from all callers at the root of each namespace. The Service Bus, however, does not publish any policy by default; it is therefore necessary to upload a Client Access Policy document to the root of your service namespace - this can be done with the ClientAccessPolicyPublisher utility included with the samples. You must ensure you have a Client Access Policy in place before running the sample or you will encounter Security Exceptions.

You must also update the class ApplicationContext to contain your Service Namespace name, Issuer Name and Issuer Secret before the sample can run. Open the file and update the code near the comment // TODO: Enter your Service Namespace information here

Sample Flow

There is some flexibility in how the sample can procede, but the following flow is recommended

  1. Before starting, be sure to open the source file ApplicationContext.cs and enter your namespace information (Service Namespace, Issuer Name, Issuer Secret) where indicated.
  2. Open the sample application in your favorite browser
  3. Type a unique name into the 'User name' box and login.
  4. The first time you login, there will be no 'Topics' listed on the left of the window. Click the green 'Add' button to create a new topic
  5. In the dialog, enter a name for the topic, confirm the 'Subscribe me to this topic' checkbox is checked and click ok
  6. The UI displays the newly created topic with a yellow star next to it - this star indicates that you are subscribed to the room.
  7. On the right, a large text area will contain messages in the chat room. There is a text box at the bottom of the window to enter new messages into.
  8. Type a message and hit 'Enter' - the message is sent to the service bus, received immediately and displayed in the large text area.
  9. Open a second browser window and navigate to the sample application again.
  10. Enter a different user name into the text box and login.
  11. Notice that the topic you created is listed on the left, but with a gray star next to it - this indicates that you are not subscribed to the room
  12. In the first browser, send another message - observe that it is received immediately in the first browser window, but not the second.
  13. In the second browser, type a message in the text box and hit enter - observe that it is received by the first browser window.
  14. In the second browser, click the gray star next to the topic name to subscribe. NOTE: this sample updates the list of topics and subscriptions in the background roughly once ever 30 seconds.
  15. One the star turns yellow, the subscription has been created - type another message in the text box and press enter
  16. Notice that the message is delivered to both the first and second browser windows
  17. Continue typing in the first and second windows as desired - messages are sent to both. Add additional browser windows (on multiple computers, if desired).
  18. Close one of the browser windows, but continue to send messages in the other
  19. Re-open the closed window and log in with the same username. Click the name of the topic that messages were sent to while you were offline and notice how they are delivered.


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