0

我刚开始玩 azure 和 service bus。我试图能够向多个客户端发送相同的消息。在其他消息传递解决方案中,如果您有多个客户端监听一个主题并且您向一个主题发送消息,他们都会收到消息。使用 Azure,只有一个订阅者会收到消息。我确定我遗漏了一些东西,任何人都可以提供一个指针吗?我的示例代码如下。

string connectionString =
"Endpoint=sb://xxx.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=xxxxxxxxxxxxxxxxxxxxxxxxxxxx=";
NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
string topic = TestTopic.Replace("/", ".").Replace("{", "-").Replace("}", "-");
if (!namespaceManager.TopicExists(topic))
{
var td = new TopicDescription(topic)
    {
    MaxSizeInMegabytes = 5120,
    DefaultMessageTimeToLive = new TimeSpan(0, 1, 0),
    };

namespaceManager.CreateTopic(td);
}


var message = new {CurrencyCode = "AUD", EffectiveDate = DateTime.Today, Rate = .97M};

TopicClient publisher = TopicClient.CreateFromConnectionString(connectionString, topic);
var bm = new BrokeredMessage("fff");
bm.Properties["Topic"] = ".xxx.fxrates.aud";

int messagesReceived = 0;
int messagesReceived2 = 0;

if (namespaceManager.SubscriptionExists(topic, "test") == false)
{
SubscriptionDescription sub = namespaceManager.CreateSubscription(topic, "test");
}

SubscriptionClient subscriber = SubscriptionClient.CreateFromConnectionString(connectionString, topic,
                                      "test",
                                      ReceiveMode.ReceiveAndDelete);
SubscriptionClient subscriber2 = SubscriptionClient.CreateFromConnectionString(connectionString, topic,
                                       "test",
                                       ReceiveMode.ReceiveAndDelete);
subscriber.OnMessage(delegate { messagesReceived++; });

subscriber2.OnMessage(delegate { messagesReceived2++; });

publisher.Send(bm);
namespaceManager.DeleteTopic(topic);
4

1 回答 1

0

首先,您必须唯一地命名您的订阅者!目前,您的两个订阅者都被命名为“test”。其次,如果您希望其他订阅者也收到消息,则必须执行 ReceiveMode.PeekLock,而不是 ReceiveMode.ReceiveAndDelete。

我知道我们开发人员讨厌 RTFM,但是当没有其他帮助时(包括 StackOverflow 问题),最好看一下它(手册):服务总线队列、主题和订阅是了解它如何工作的好来源。以及如何使用服务总线主题/订阅将带您到正确的点。

于 2013-09-26T07:31:31.247 回答