我正在尝试组装一个简单的 AppFabric 主题,其中使用 SessionId 发送和接收消息。代码不会中止,但brokeredMessage始终为空。这是代码:
// BTW, the topic already exists
var messagingFactory = MessagingFactory.Create(uri, credentials);
var topicClient = messagingFactory.CreateTopicClient(topicName);
var sender = topicClient.CreateSender();
var message = BrokeredMessage.CreateMessage("Top of the day!");
message.SessionId = "1";
sender.Send(message);
var subscription = topic.AddSubscription("1", new SubscriptionDescription { RequiresSession = true});
var mikeSubscriptionClient = messagingFactory.CreateSubscriptionClient(subscription);
var receiver = mikeSubscriptionClient.AcceptSessionReceiver("1");
BrokeredMessage brokeredMessage;
receiver.TryReceive(TimeSpan.FromMinutes(1), out brokeredMessage); // brokeredMessage always null
最佳答案
您的代码中有两个问题:
您在发送消息之后创建订阅。您需要在发送之前创建订阅,因为订阅告诉主题在某种意义上将消息复制到几个不同的“存储桶”。
您正在使用 TryReceive,但没有检查其结果。如果收到消息,则返回 true;如果未收到消息,则返回 false(例如发生超时)。
我正在编写示例应用程序,并将于今天将其发布在我们的博客上。我也会在这里发布链接。但在那之前,将订阅逻辑移至发送消息之前,并将接收者移至其之后,您将开始看到结果。
更新: 正如所 promise 的,这是我的博客文章的链接 getting started with AppFabric Queues, Topics, Subscriptions.
关于AppFabric主题订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485623/