Azure Pub / Sub几乎正常工作

时间:2016-07-24 06:43:01

标签: c#

我正在尝试为许多Subscriber原型创建一个One Publisher。

订阅者可靠地接收消息;直到我实例化第二个订户。在这种情况下,每个订户轮流接收订阅,而另一个订户实例不接收消息。希望看到每个客户收到一条消息后发布的消息。

这是订阅者。

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

1 个答案:

答案 0 :(得分:0)

您需要拥有多个订阅,每个订阅者一个订阅。单个订阅将像队列一样,只允许一个消费者接收每条消息。