I am trying to filter the messages based on Client Id under the subscription by using SQL Filter in Azure Service Bus Topic. Even after applying the SQL filter, we are still getting other Client Id related messages under the subscription. Could you please help us to resolve this issue?
Make sure you are adding the SqlFilter properly like below with correct payload message:-
I have created one subscription and added the Filter below:-
Message Body payload:-
{
"orderId": "124",
"color": "blue",
"ClientId": "c0c952e9-5254-45b5-b838-6d26a31435cb",
"priority": "medium"
}
color='blue' AND ClientId='c0c952e9-5254-45b5-b838-6d26a31435cb'
Output:-
C# code to perform the operation:-
using Azure.Messaging.ServiceBus;
using System;
using System.Threading.Tasks;
public class SendMessages
{
static string connectionString = "Endpoint=sb://silicon9.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxxxxx89vA+ASbB60thA=";
static string topicName = "valleytopic";
public static async Task Main()
{
await using var client = new ServiceBusClient(connectionString);
ServiceBusSender sender = client.CreateSender(topicName);
var message1 = new ServiceBusMessage(new BinaryData(@"{
""orderId"": ""123"",
""color"": ""blue"",
""quantity"": 5,
""priority"": ""low""
}"));
var message2 = new ServiceBusMessage(new BinaryData(@"{
""orderId"": ""124"",
""priority"": ""medium""
}"));
message2.ApplicationProperties["color"] = "blue";
message2.ApplicationProperties["ClientId"] = "c0c952e9-5254-45b5-b838-6d26a31435cb";
var message3 = new ServiceBusMessage(new BinaryData(@"{
""orderId"": ""125"",
""priority"": ""high""
}"));
message3.ApplicationProperties["user.color"] = "red";
message3.ApplicationProperties["quantity"] = 20;
var message4 = new ServiceBusMessage(new BinaryData(@"{
""orderId"": ""126"",
""color"": ""red"",
""quantity"": 15
}"));
message4.Subject = "red";
message4.CorrelationId = "high";
await sender.SendMessageAsync(message1);
await sender.SendMessageAsync(message2);
await sender.SendMessageAsync(message3);
await sender.SendMessageAsync(message4);
Console.WriteLine("Messages sent.");
}
}
Output:-
When I peeked message:-