Azure ServiceBus上的SqlFilter主题订阅未过滤

时间:2012-07-24 21:17:10

标签: azure servicebus

我有一个WinRT应用程序,我正在使用Windows Azure Toolkit for Windows 8。我有一个设置,我希望客户订阅忽略发布到ServiceBus主题的消息,如果他们是发起人或者消息比他们的订阅开始时更早。

在我的BrokeredMessage的属性中,我添加了两个项目来涵盖这些场景:

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!  :(
    }

我找回了所有东西 - 所以我不确定我做错了什么。解决订阅过滤器问题的最简单方法是什么?

2 个答案:

答案 0 :(得分:13)

创建订阅时,默认情况下会获得“MatchAll”过滤器。在上面的代码中,您只是添加了过滤器,因此除了“MatchAll”过滤器之外,还会应用它,因此会收到所有消息。只需在创建订阅后删除$ Default过滤器,即可解决问题。

答案 1 :(得分:1)

排除故障的最佳方法是使用Paolo Salvatori的Service Bus Explorer http://code.msdn.microsoft.com/windowsazure/Service-Bus-Explorer-f2abca5a

他在博客上做了很多博文,例如http://windowsazurecat.com/2011/07/exploring-topics-and-queues-by-building-a-service-bus-explorer-toolpart-1/

Windows Azure SDK 1.7确实具有内置功能,但Service Bus Explorer独立版本仍然更好,请参阅此处的比较。

http://soa-thoughts.blogspot.com.au/2012/06/visual-studio-service-bus-explorer.html

HTH你的调试......