Bu sayfa faydalı mıydı?
Bu içerik hakkındaki geri bildiriminiz önemli. Ne düşündüğünüzü bilmemize izin verin.
İlave geri bildirim?
1500 karakter kaldı
Dışarıya aktar (0) Yazdır
Tümünü Genişlet
EN
Bu içerik dilinizde bulunmamaktadır ancak İngilizce sürümüne buradan bakabilirsiniz.

Step 7: Create a Topic and Subscription

Updated: May 1, 2015

This is the seventh of nine tasks required to create a basic REST-style queue and publication/subscription application that uses Microsoft 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.

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

    // 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);
    }
    

  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);
    }
    

Show:
© 2015 Microsoft