2016-07-24 52 views
0

我正在嘗試爲許多訂閱者原型創建一個發佈者。Azure Pub/Sub幾乎可以工作

訂戶正在可靠地接收消息;直到我實例化第二個用戶。在這種情況下,每個用戶輪流接收預訂,而另一個用戶實例不接收消息。希望看到每個客戶端收到每條消息後都會發布消息。

這是用戶。

using Microsoft.ServiceBus; 
using Microsoft.ServiceBus.Messaging; 
using System; 
using Microsoft.Azure; // Namespace for CloudConfigurationManager 
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount 
using Microsoft.WindowsAzure.Storage.Queue; // Namespace for Queue storage types 
namespace TestReader 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
       CloudConfigurationManager.GetSetting("StorageConnectionString")); 
      string connectionString = 
        CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"); 
      var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); 

      if (!namespaceManager.TopicExists("TestTopic")) 
      { 
       namespaceManager.CreateTopic("TestTopic"); 
      } 
      if (!namespaceManager.SubscriptionExists("TestTopic", "AllMessages")) 
      { 
       namespaceManager.CreateSubscription("TestTopic", "AllMessages"); 
      } 

      ; 
      SubscriptionClient subClient = SubscriptionClient.CreateFromConnectionString 
               (connectionString, "TestTopic", "AllMessages"); 

      subClient.OnMessage(message => 
      { 
       Console.WriteLine(String.Format("Message body: {0}", message.GetBody<String>())); 
       Console.WriteLine(String.Format("Message id: {0}", message.MessageId)); 
      }); 
      Console.WriteLine("Press [Enter] to Exit"); 
      Console.ReadLine(); 
     } 
    } 
} 

這裏是出版商:

using Microsoft.ServiceBus; 
using Microsoft.ServiceBus.Messaging; 
using System; 
using Microsoft.Azure; // Namespace for CloudConfigurationManager 
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount 

namespace TestWriter 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     {   
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString")); 
      string connectionString = 
        CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"); 
      var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); 
      if (!namespaceManager.TopicExists("TestTopic")) 
      { 
       namespaceManager.CreateTopic("TestTopic"); 
      } 

      TopicClient topicClient = 
       TopicClient.CreateFromConnectionString(connectionString, "TestTopic"); 
      SubscriptionClient subClient = SubscriptionClient.CreateFromConnectionString 
      (connectionString, "TestTopic", "AvailableMsgs"); 

      Console.WriteLine("Please press 'S' to send 'X' to Exit"); 
      char s = 'S'; 
      do 
      { 
       s = Console.ReadKey().KeyChar; 
       if (s == 'S') 
       { 
        var message = new BrokeredMessage("This is a test message!"); 
        topicClient.Send(message); 
        Console.WriteLine("==>Sent"); 
       } 
      } while (s == 'S'); 
     } 
    } 
} 

回答

0

你需要有多個預訂,每一個消費者。單個訂閱將充當隊列,並且僅允許一個消費者接收每條消息。

相關問題