0
votes

I'm new to azure service bus and masstransit. I'm looking for a solution to a specific situation.

I have a azure service bus topic with multiple subscribers. Subscriber will receive message based on filters. I've created the topic and subscriber with code below

    class Program
        {
            static void Main(string[] args)
            {
    
                string connectionString = "Endpoint connection string";
    
                // the names of topics and subscriptions we'll be working with
                const string topicName = "MyTestTopic";
                const string allMessagesSubName = "AllMessages";
                const string filteredSubName1 = "Filtered1";
                const string filteredSubName2 = "Filtered2";
    
                // let's create the topic if it doesn't already exist...
                var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
                if (!namespaceManager.TopicExists(topicName))
                {
                    var td = new TopicDescription(topicName);
                    namespaceManager.CreateTopic(td.Path);
                }
                if (!namespaceManager.SubscriptionExists(topicName, allMessagesSubName))
                {
                    namespaceManager.CreateSubscription(topicName, allMessagesSubName);
                }
    
                if (!namespaceManager.SubscriptionExists(topicName, filteredSubName1))
                {
                    namespaceManager.CreateSubscription(
                        new SubscriptionDescription(topicName, filteredSubName1),
                        new Microsoft.ServiceBus.Messaging.SqlFilter("From LIKE '%Smith'"));
                }
    
                if (!namespaceManager.SubscriptionExists(topicName, filteredSubName2))
                {
                    namespaceManager.CreateSubscription(
                        new SubscriptionDescription(topicName, filteredSubName2),
                        new Microsoft.ServiceBus.Messaging.SqlFilter("sys.Label='important'"));
                }
    
                var message1 = new BrokeredMessage("Hello World");
    
                var message2 = new BrokeredMessage("Second message");
                message2.Label = "important";
    
                var message3 = new BrokeredMessage("Third message");
                message3.Properties["From"] = "Kelly Smith";
                message3.Label = "information";
    
                var client = TopicClient.CreateFromConnectionString(connectionString, topicName);
                client.Send(message1);
                client.Send(message2);
                client.Send(message3);
                client.Close();
            }
        }

Here in the code we're adding Message custom properties like From.

Now I want to send such message using masstransit. In masstransit I cannot find any option of adding Message custom properties using the Publish() method. Is there any way that I can send these messages using masstransit where these filters can be used?

NB: I've read the answer of this question But the anwer here tells us to filter the messages in subscriber side. What I want is that this filtering will occur before reaching the subscriber.

1

1 Answers

2
votes

When using Azure Service Bus with MassTransit, you can add subscription endpoints in additional to regular endpoints. When configuring a subscription endpoint, you should be able to specify rules and/or filters as part of the subscription. Which is exactly what you're doing above, so that is handled.

The other part, adding properties to the message, can be done by adding text headers to the SendContext. Those headers are copied to the message Properties collection, which I believe can be used to filter messages using a "SQL" filter (which is configured on the subscription endpoint, or the topic subscription on a receive endpoint).