Export (0) Print
Expand All

Step 2: Create a Console Client

Updated: January 22, 2014

This is the second of nine tasks required to create a basic REST-style queue and publication/subscription application that uses 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 Active Directory Access Control (also known as Access Control Service or 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 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.

To create a console application

  1. Open Visual Studio as an administrator by right-clicking the program in the Start menu and clicking 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 + "/";
        // 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 msg = ReceiveAndDeleteMessage(queueName);
        string topicName = "Topic" + Guid.NewGuid().ToString();
        string subscriptionName = "Subscription" + Guid.NewGuid().ToString();
        CreateSubscription(topicName, subscriptionName);
        SendMessage(topicName, "msg2");
        // Wait for messages to post:
        Console.WriteLine(ReceiveAndDeleteMessage(topicName + "/Subscriptions/" + subscriptionName));
        // Get an Atom feed with all the queues in the namespace
        // Get an Atom feed with all the topics in the namespace
        // 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
        // Delete the topic we created
        // Get an Atom feed with all the topics in the namespace, it shouldn't have the one we created now
        // Get an Atom feed with all the queues in the namespace, it shouldn't have the one we created now
    catch (WebException we)
        using (HttpWebResponse response = we.Response as HttpWebResponse)
            if (response != null)
                Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
    Console.WriteLine("\nPress ENTER to exit.");

Community Additions

© 2014 Microsoft