Service Bus Brokered Messaging REST Tutorial
Step 2: Create a Console Client
This is the second of nine tasks required to create a basic REST-style queue and publication/subscription application that uses the Windows Azure Service Bus.
Service Bus queues enable you to store messages in a first-in, first-out queue. Topics and subscriptions implement a publish/subscribe pattern; you create a topic and then create one or more subscriptions associated with that topic. When messages are sent to the topic, they are immediately sent to the subscribers of that topic.
The code in this tutorial:
• Uses your service namespace, issuer name, and issuer key to contact the Windows Azure Access Control Service (ACS) to obtain a Simple Web Token (SWT) to gain access to your Service Bus service namespace resources.
• Creates a queue, sends a message to the queue, and reads the message from the queue. • Creates a topic, a subscription to that topic, and sends and reads the message from the
subscription.
• Retreives all the queue, topic, and subscription information – including subscription rules -- from the Service Bus for your service namespace.
• It then deletes the queue, topic, and subscription resources.
Because the service is a REST-style Web service, there are no special types involved, as the entire exchange involves strings. This means that the Visual Studio project must make no references other than the defaults, although if your configuration has modified the defaults, you may have to add some basic .NET Framework references to the code.
After obtaining the service namespace and credentials in step 1, the next step is to create a basic Visual Studio console application.
1. Open Visual Studio 2010 as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.
2. Create a new console application project. Click the File menu and select New, Project. In the New Project dialog, select Visual C# (if Visual C# does not appear, look under Other Languages), select the Console Application template, and name it
Microsoft.ServiceBus.Samples. Use the default Location. Click OK to create the project.
3. For a C# project, Visual Studio creates a file that is named Program.cs. This class will
contain an empty method called Main().This method is required for a console application
project to build correctly. Therefore, you can safely leave it in the project. 4. Make sure your using statements appear as follows:
using System;
using System.Collections.Specialized; using System.IO;
using System.Net;
using System.Text; using System.Xml;
5. If necessary, rename the service namespace for the program from the Visual Studio default to Microsoft.ServiceBus.Samples.
6. Inside the Program class, add the following global variables:
static string serviceNamespace; static string baseAddress; static string token;
const string sbHostName = "servicebus.windows.net"; const string acsHostName = "accesscontrol.windows.net";
7. Inside the Main() method, copy the following code:
Console.Write("Enter your service namespace: "); serviceNamespace = Console.ReadLine();
Console.Write("Enter your issuer name: "); string issuerName = Console.ReadLine();
Console.Write("Enter your issuer secret: "); string issuerSecret = Console.ReadLine();
baseAddress = "https://" + serviceNamespace + "." + sbHostName + "/";
try {
// Get a SWT token from the Access Control Service, given the issuerName and issuerSecret values.
token = GetToken(issuerName, issuerSecret);
string queueName = "Queue" + Guid.NewGuid().ToString();
// Create and put a message in the queue using the SWT token.
CreateQueue(queueName, token); SendMessage(queueName, "msg1");
string topicName = "Topic" + Guid.NewGuid().ToString(); string subscriptionName = "Subscription" +
Guid.NewGuid().ToString(); CreateTopic(topicName);
CreateSubscription(topicName, subscriptionName); SendMessage(topicName, "msg2");
// Wait for messages to post:
//System.Threading.Thread.Sleep(500);
Console.WriteLine(ReceiveAndDeleteMessage(topicName + "/Subscriptions/" + subscriptionName));
// Get an Atom feed with all the queues in the namespace Console.WriteLine(GetResources("$Resources/Queues"));
// Get an Atom feed with all the topics in the namespace Console.WriteLine(GetResources("$Resources/Topics"));
// Get an Atom feed with all the subscriptions for the topic we just created
Console.WriteLine(GetResources(topicName + "/Subscriptions"));
// Get an Atom feed with all the rules for the topic and subscritpion we just created
Console.WriteLine(GetResources(topicName + "/Subscriptions/" + subscriptionName + "/Rules"));
// Delete the queue we created DeleteResource(queueName);
// Delete the topic we created DeleteResource(topicName);
// Get an Atom feed with all the topics in the namespace, it shouldn't have the one we created now
Console.WriteLine(GetResources("$Resources/Topics"));
// Get an Atom feed with all the queues in the namespace, it shouldn't have the one we created now
Console.WriteLine(GetResources("$Resources/Queues")); }
catch (WebException we) {
using (HttpWebResponse response = we.Response as HttpWebResponse) { if (response != null) { Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd()); } else { Console.WriteLine(we.ToString()); } } }
Console.WriteLine("\nPress ENTER to exit."); Console.ReadLine();
Step 3: Create Management Credentials
This is the third of nine tasks required to create a basic REST-style queue and publication/subscription application that uses the Service Bus.
The next step is to write a method that processes the service namespace, issuer name, and issuer secret that you entered in the previous step, and returns a Simple Web Token (SWT).
1. Paste the following code after the Main() method in the Program class:
private static string GetToken(string issuerName, string issuerSecret)
{
var acsEndpoint = "https://" + serviceNamespace + "-sb." + acsHostName + "/WRAPv0.9/";
// Note that the realm used when requesting a token uses the HTTP scheme, even though
// calls to the service are always issued over HTTPS var realm = "http://" + serviceNamespace + "." + sbHostName + "/";
NameValueCollection values = new NameValueCollection(); values.Add("wrap_name", issuerName);
values.Add("wrap_password", issuerSecret); values.Add("wrap_scope", realm);
WebClient webClient = new WebClient();
byte[] response = webClient.UploadValues(acsEndpoint, values);
string responseString =
Encoding.UTF8.GetString(response);
var responseProperties = responseString.Split('&'); var tokenProperty = responseProperties[0].Split('='); var token = Uri.UnescapeDataString(tokenProperty[1]);
return "WRAP access_token=\"" + token + "\""; }