Czy ta strona była przydatna?
Twoja opinia na temat zawartości jest dla nas bardzo ważna. Powiedz nam co myślisz.
Dodatkowe opinie?
Pozostało znaków: 1500
Eksportuj (0) Drukuj
Rozwiń wszystko
Ta zawartość nie jest dostępna w wymaganym języku. Wersja w języku angielskim znajduje się tutaj.

Step 2: Create a Console Client

Updated: May 1, 2015

This is the second of nine tasks required to create a basic REST-style queue and publication/subscription application that uses Microsoft 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 and Shared Access Signature (SAS) key to 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 to any Service Bus specific libraries.

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 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 click New, then click 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 named Program.cs. This class contains an empty Main() method, which 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.Globalization;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography;
    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 = "";
  7. Inside the Main() method, paste the following code:

    Console.Write("Enter your service namespace: ");
    serviceNamespace = Console.ReadLine();
    Console.Write("Enter your SAS key: ");
    string SASKey = Console.ReadLine();
    baseAddress = "https://" + serviceNamespace + "." + sbHostName + "/";
        token = GetSASToken("RootManageSharedAccessKey", SASKey);
        string queueName = "Queue" + Guid.NewGuid().ToString();
        // Create and put a message in the queue
        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");
        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 subscription 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.");

© 2015 Microsoft