AppFabric主题订阅

发布于 2024-11-17 09:12:41 字数 819 浏览 3 评论 0原文

我正在尝试组装一个简单的 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

I am trying to assemble a simple AppFabric Topic whereby messages are sent and received using the SessionId. The code does not abort, but brokeredMessage is always null. Here is the code:

// 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独行侠 2024-11-24 09:12:41

您的代码中有两个问题:

  1. 发送消息之后创建订阅。您需要在发送之前创建订阅,因为订阅告诉主题在某种意义上将消息复制到几个不同的“存储桶”。

  2. 您正在使用 TryReceive,但没有检查其结果。如果收到消息则返回 true,否则返回 false(例如发生超时)。

我正在编写示例应用程序,并将于今天将其发布在我们的博客上。我也会在这里发布链接。但在那之前,将订阅逻辑移至发送消息之前,并将接收者移至其之后,您将开始看到结果。

更新:
正如所承诺的,这里是我关于 AppFabric 入门的博客文章的链接队列、主题、订阅。

You have two problems in your code:

  1. You create a subscription AFTER you send the message. You need to create a subscription before sending, because a subscription tells the topic to, in a sense, copy, the message to several different "buckets".

  2. You are using TryReceive but are not checking for its result. It returns true, if a message was received, and false if not (e.g. Timeout has occured).

I am writing my sample application and will post it on our blog today. I will post the link here as well. But until then, move the subscription logic to before sending the message, and the receiver after it and you will start seeing results.

Update:
As promised, here is the link to my blog post on getting started with AppFabric Queues, Topics, Subscriptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文