Eksportuj (0) Drukuj
Rozwiń wszystko
EN
Ta zawartość nie jest dostępna w wymaganym języku. Wersja w języku angielskim znajduje się tutaj.

Step 7: Create a Topic and Subscription

Updated: January 22, 2014

This is the seventh of nine tasks required to create a basic REST-style queue and publication/subscription application that uses Windows Azure Service Bus.

The next step is to write a method that uses the REST-style HTTP PUT command to create a topic. Then, you write a method that creates a subscription to that topic.

To create a topic

  1. Paste the following code directly beneath the ReceiveAndDeleteMessage() code you added in step 6:

    // Creates a Topic with the given topic name and the SWT token
    // Using an HTTP PUT request.
    private static string CreateTopic(string topicName)
    {
        var topicAddress = baseAddress + topicName;
        WebClient webClient = new WebClient();
        webClient.Headers[HttpRequestHeader.Authorization] = token;
    
        Console.WriteLine("\nCreating topic {0}", topicAddress);
        // Prepare the body of the create queue request
        var putData = @"<entry xmlns=""http://www.w3.org/2005/Atom"">
                                      <title type=""text"">" + topicName + @"</title>
                                      <content type=""application/xml"">
                                        <TopicDescription xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"" />
                                      </content>
                                    </entry>";
    
        byte[] response = webClient.UploadData(topicAddress, "PUT", Encoding.UTF8.GetBytes(putData));
        return Encoding.UTF8.GetString(response);
    }
    

To create a subscription

  1. The following code creates a subscription to the topic you created in the previous section. Add the following code directly beneath the CreateTopic() definition:

    private static string CreateSubscription(string topicName, string subscriptionName)
    {
        var subscriptionAddress = baseAddress + topicName + "/Subscriptions/" + subscriptionName;
        WebClient webClient = new WebClient();
        webClient.Headers[HttpRequestHeader.Authorization] = token;
    
        Console.WriteLine("\nCreating subscription {0}", subscriptionAddress);
        // Prepare the body of the create queue request
        var putData = @"<entry xmlns=""http://www.w3.org/2005/Atom"">
                                      <title type=""text"">" + subscriptionName + @"</title>
                                      <content type=""application/xml"">
                                        <SubscriptionDescription xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"" />
                                      </content>
                                    </entry>";
    
        byte[] response = webClient.UploadData(subscriptionAddress, "PUT", Encoding.UTF8.GetBytes(putData));
        return Encoding.UTF8.GetString(response);
    }
    

Zawartość społeczności

Dodaj
Pokaż:
© 2014 Microsoft