I am trying to use Azure service bus to pass messages between multiple azure functions. I am creating topic and subscription in code. But not sure how to add filter query from code. I want to filter messages by id . e.g SqlFilter($"'id' ='{id}'")
. I don't want to create one subscription for each message because of this filter.
Is it possible to have different filter for a subscription?
This issue can be fixed by creating one subscription for each request and deleting it afterwards. But i don't want to create so many subscriptions. My aim is to just change the filter each time the function is called.
Sample code:
var connectionString = ConfigurationManager.AppSettings["ServiceBus_CONNECTION_STRING"];
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!await namespaceManager.TopicExistsAsync(topicName))
{
// Configure Topic Settings.
var topic = new TopicDescription(topicName)
{
MaxSizeInMegabytes = 1024,
DefaultMessageTimeToLive = TimeSpan.FromMinutes(5)
};
await namespaceManager.CreateTopicAsync(topic);
}
if (!await namespaceManager.SubscriptionExistsAsync(topicName, subscription))
{
await namespaceManager.CreateSubscriptionAsync(topicName, subscription);
}
var cts = new TaskCompletionSource<T>();
var subClient =
SubscriptionClient.CreateFromConnectionString(connectionString, topicName,
subscription);
var ruleDescription = new RuleDescription($"RequestIdRule{id}", new SqlFilter($"'id' ='{id}'"));
await subClient.AddRuleAsync(ruleDescription);
var options = new OnMessageOptions();
subClient.OnMessage(m =>
{
try
{
var body = m.GetBody<T>();
cts.SetResult(body);
}
catch (Exception ex)
{
m.Abandon();
}
},
options);
return await cts.Task;
}
catch (Exception ex)
{
throw;
}