Unable to filter service bus topic messages using SqlFilter

350 Views Asked by At

I've subscribed to a topic and the sender sets two UserProperties 'Classification' and 'SubClassification'. I want to filter the messages with Classification set to 1. I tried adding the following SqlFilter.

SqlFilter("Classification='1'")

It doesn't work. I still receive all the messages irrespective of the 'Classification' property.

I'm using the Subscription client from Microsoft.Azure.ServiceBus namespace.

2

There are 2 best solutions below

1
On

You don't need to use SqlFilter() inside the Rule. Just setting it as Classification='1' will filter the messages.

0
On

Try this to debug: (it will always match initially, but then you get rid of the

OR 1=1

after things are working. .

RuleDescription rd = new RuleDescription();
rd.Filter = new SqlFilter("CustomProperty = '1' OR 1=1");

Then you can tweak the syntax sugar as needed.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.sqlfilter?view=azure-dotnet

You should show YOUR code on how you create the subscription. Below is some code.. as an example:

NamespaceManager nsm = /* outside of scope of this question */
SubscriptionDescription currentSubscriptionDescription = new SubscriptionDescription("MyTopicPath", "MySubscriptionName");
currentSubscriptionDescription.AutoDeleteOnIdle = false;
SubscriptionDescription postCreateSd = nsm.CreateSubscription(currentSubscriptionDescription);


/* now tweak it */
MessagingFactory mf = /* outside of scope of this question */
SubscriptionClient subClient = mf.CreateSubscriptionClient("MyTopicPath", "MySubscriptionName");
RuleDescription rd = new RuleDescription();
rd.Filter = new SqlFilter("CustomProperty = '1' OR 1=1");
subClient.AddRule(rd);