0
votes

I want to create filter rule via CorrelationFilter for subscriptions associated with a Topic, as it is faster than SQLFilter.

The rule: any message that contains a header that equals to a string will go to one subscription, another string will go to different subscription. For example:

Topic: order
Subcription1: header_orderType: orderPlaced
Subcription2: header_orderType: orderPaid

Similar to the one highlighted in blue below via Service Bus Explorer.

enter image description here Below is other ways that can acheive that.

SQLFilter in code https://dzone.com/articles/everything-you-need-know-about-5

SQLFilter https://github.com/Azure/azure-service-bus/tree/master/samples/DotNet/Microsoft.Azure.ServiceBus/TopicFilters

PS https://docs.microsoft.com/en-us/powershell/module/azurerm.servicebus/New-AzureRmServiceBusRule?view=azurermps-6.13.0

1

1 Answers

3
votes

The TopicFilters sample covers correlation filter too which is setup using an ARM template. The same should be possible in C# and PS as well.

C#

You will have to first create a Microsoft.Azure.ServiceBus.CorrelationFilter object

var orderPlacedFilter = new CorrelationFilter();

filter.Properties["header_orderType"] = "orderPlaced";

And then add it to your subscription client object by calling Microsoft.Azure.ServiceBus.SubscriptionClient.AddRuleAsync()

subsClient.AddRuleAsync("orderPlacedFilter", orderPlacedFilter);

Similarly, for the other subscription and its filter.

PowerShell

Guess the documentation isn't really great on this one but I believe this should work

$rule = New-AzServiceBusRule -ResourceGroupName prvalav-common -Namespace prvalav-common -Topic test -Subscription test -Name SBRule -SqlExpression "test = 0"

$rule.FilterType = 1
$rule.SqlFilter = $null
$rule.CorrelationFilter.Properties["header_orderType"] = "orderPlaced"

Set-AzServiceBusRule -ResourceGroupName prvalav-common -Namespace prvalav-common -Topic test -Subscription test -Name SBRule -InputObject $rule

If you were wondering about the FilterType = 1, check the FilterType enum.

After setting this up, in your function app, you would just use the Service Bus Trigger with the topic/subscription details.