我有一个 WinRT 应用程序,正在使用适用于 Windows 8 的 Windows Azure Toolkit。我有一个设置,我希望订阅的客户端忽略发布到 ServiceBus 主题的消息(如果他们是发起者或者消息早于订阅开始时的消息)。
在我的 BrokeredMessage 的属性中,我添加了 2 个项目来涵盖这些场景:
message.Properties["Timestamp"] = DateTime.UtcNow.ToFileTime();
message.Properties["OriginatorId"] = clientId.ToString();
clientId 是一个 Guid。
订阅者端如下所示:
// ti is a class that contains a Topic, Subscription and a bool as a cancel flag.
string FilterName = "NotMineNewOnly";
// Find or create the topic.
if (await Topic.ExistsAsync(DocumentId.ToString(), TokenProvider))
{
ti.Topic = await Topic.GetAsync(DocumentId.ToString(), TokenProvider);
}
else
{
ti.Topic = await Topic.CreateAsync(DocumentId.ToString(), TokenProvider);
}
// Find or create this client's subscription to the board.
if (await ti.Topic.Subscriptions.ExistsAsync(ClientSettings.Id.ToString()))
{
ti.Subscription = await ti.Topic.Subscriptions.GetAsync(ClientSettings.Id.ToString());
}
else
{
ti.Subscription = await ti.Topic.Subscriptions.AddAsync(ClientSettings.Id.ToString());
}
// Find or create the subscription filter.
if (!await ti.Subscription.Rules.ExistsAsync(FilterName))
{
// Want to ignore messages generated by this client and ignore any that are older than Timestamp.
await ti.Subscription.Rules.AddAsync(FilterName, sqlFilterExpression: string.Format("(OriginatorId != '{0}') AND (Timestamp > {1})", ClientSettings.Id, DateTime.UtcNow.ToFileTime()));
}
ti.CancelFlag = false;
Topics[boardId] = ti;
while (!ti.CancelFlag)
{
BrokeredMessage message = await ti.Subscription.ReceiveAndDeleteAsync(TimeSpan.FromSeconds(30));
if (!ti.CancelFlag && message != null)
{
// Everything gets here! :(
}
我拿回了所有东西——所以我不确定我做错了什么。解决订阅过滤器问题的最简单方法是什么?
最佳答案
当您创建订阅时,默认情况下您会获得“MatchAll”过滤器。在上面的代码中,您只需添加过滤器,以便除了“MatchAll”过滤器之外还应用它,从而收到所有消息。创建订阅后,只需删除 $Default 过滤器即可解决问题。
关于azure - Azure ServiceBus 上的 SqlFilter 主题订阅未过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11639437/