我刚开始玩 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);