Using multiple CorrelationFilter objects in a Windows Azure Service Bus subscription

Using filters in Windows Azure Service Bus subscriptions is very useful when it comes to message categorization.  When setting up a subscription, you can specify a Filter object that will be responsible of allowing messages that match a specified criteria.  In this post I will focus on how we can set multiple CorrelationFilter objects for a Service Bus subscription.

Correlation filters help to establish a call-response type of relationship between a sender and a receiver through the use of the CorrelationId property of the BrokeredMessage class.  Typically, the sender might set the CorrelationId property of the brokered message to some identifier.  This same identifier can be used in a CorrelationFilter object associated to that particular sender when creating a subscription for the response topic.

var id = Guid.NewGuid();
var filter = new CorrelationFilter(id.ToString());
namespaceClient.CreateSubscription(description, filter);

The receiver, after getting the message,  will prepare a new response, setting the CorrelationId property equal to the one it received in the message it downloaded, and will post the response to a topic that will be shared by multiple sender entities.  Each sender will always receive the expected response through the associated CorrelationFilter (which delivers the message to the subscription only if the CorrelationId property of the brokered message matches the id expression in the filter.  Tip: consider the use of CorrelationFilter instead of SqlFilter when it comes to equality comparisons.  SqlFilter has an extra overhead when evaluating the criteria expression.

Consider the following scenario:

  • We have n senders posting messages to a topic, allowing for m subscriptions.
  • We have m receivers subscribed to the sender topic, posting reply messages back to a shared reply topic.
  • A sender may want to make multiple requests and expect the respective replies to each request.
  • Each reply message may be treated differently by the sender.

The question is, how can a sender keep track of the requests it makes, and how can it receive the expected reply message without the knowledge of the other senders?

The answer is as simple as… “let the sender manage a collection of correlation filters! ”  We can achieve this through the use of Rules.  A rule allows a subscription to execute an action on messages that match a rule’s filtering criteria.  Rules can be managed through the SubscriptionClient class.  Let’s take a look at the code.

Through the MessagingFactory we create a SubscriptionClient.  We then call the AddRule method, creating a new RuleDescription object.  The RuleDescription will contain the name or identifier of that rule, and the corresponding associated CorrelationFilter.  Using the RuleAction property escapes the scope of this post, but you can read about it here.

// Create new rule containing a new CorrelationFilter.
var subscriptionClient = this.messagingFactory.CreateSubscriptionClient(Constants.ReplyTopic, this.senderName);

var rule = new RuleDescription
Filter = new Microsoft.ServiceBus.Messaging.CorrelationFilter(id.ToString()),
Name = id.ToString(),


When we are done with rule, we can call the RemoveRule method of the SubscriptionClientClass.

Note that we can use Windows Azure Service Bus for a wide variety of scenarios, and solve similar problems in many different ways.  Today we went down the path of subscriptions and correlation filters.

Check out these other posts if you want more information on Windows Azure Service Bus Brokered Messaging

Happy learning!

One Comment

Leave a Reply