根据您的描述,我检查了 C# 的 Service Bus 客户端库,发现在通过NamespaceManager.CreateSubscription创建订阅时,我们只能通过单个Filter
或RuleDescription
单个过滤器,如下所示:
public SubscriptionDescription CreateSubscription(string topicPath, string name, Filter filter);
public SubscriptionDescription CreateSubscription(string topicPath, string name, RuleDescription ruleDescription);
正如 Sean Feldman 提到的创建复杂的 SQL 过滤器,我创建了我的应用程序来测试它。你可以参考我的测试如下:
示例消息
var body = "Hello World";
var message1 = new BrokeredMessage(body);
message1.Properties["From"] = "Ian Smith";
message1.Properties["MessageId"] = -1;
message1.Label = null;
var message2 = new BrokeredMessage("Second message");
message2.Properties["From"] = "Alan Smith";
message2.Label = "important";
message2.Properties["MessageId"] = 2;
var message3 = new BrokeredMessage("Third message");
message3.Properties["From"] = "Kelly Smith";
message3.Label = "information";
message3.Properties["MessageId"] = 1;
使用过滤器创建订阅
var filter=new SqlFilter("(sys.Label='important' or sys.Label IS NOT NULL) or MessageId<0 or From LIKE '%Smith'");
namespaceManager.CreateSubscription(topicName, subName,filter);
上述订阅可以接收所有三个消息。有关更多详细信息,您可以参考SQLFilter 语法。
更新:
对于接收消息,您可以利用SubscriptionClient.OnMessage并参考以下代码片段:
//Processes a message in an event-driven message pump.
client.OnMessage((receivedMessage)=>{
Console.WriteLine(receivedMessage.Properties['id'].ToString());
receivedMessage.Complete(); //manually mark the message as processed and deleted.
},new OnMessageOptions(){
AutoComplete=false,
MaxConcurrentCalls=5
});