sppc42
sppc42

Reputation: 3292

Azure Service Bus - SQLFilter doesn't works as expected

Am trying to run the Azure Service Bus sample given in their official documentation at https://azure.microsoft.com/en-gb/documentation/articles/service-bus-dotnet-how-to-use-topics-subscriptions/.

Creating subscriptions

namespaceManager.CreateSubscription(TopicName, "CreatedMessages", new SqlFilter(@"Type = 'Created'"));

Sending Messages

public void SendMessages()
{
    var client = TopicClient.CreateFromConnectionString(_connectionString, TopicName);

    for (var i = 0; i < 5; i++)
    {
        var message = new BrokeredMessage("TestMessage " + i);

        if (i%2 == 0)
            message.Properties["Type"] = "Created";
        else
            message.Properties["Type"] = "All";

        client.Send(message);
    }
}

Handling Messages

    public void ListenMessages()
    {
        Task.Factory.StartNew(() => SubscribeMessages("CreatedMessages"));
    }

    public void SubscribeMessages(string subscription)
    {
        var allMessagesClient = SubscriptionClient.CreateFromConnectionString(_connectionString, TopicName, subscription);

        allMessagesClient.OnMessage(message =>
        {
            try
            {
                Console.WriteLine($"** {subscription} **");
                Console.WriteLine("Body: " + message.GetBody<string>());
                Console.WriteLine("MessageID: " + message.MessageId);
                Console.WriteLine("Message Type: " + message.Properties["Type"]);
                Console.WriteLine();

                message.Complete();
            }
            catch (Exception)
            {
                message.Abandon();
            }
        },
        new OnMessageOptions
        {
            AutoComplete = false,
            AutoRenewTimeout = TimeSpan.FromMinutes(1)
        });
    }

The above code works as expected.

However, problem am facing is that the moment the 'Type' property is renamed to anything else, like 'MessageType' or 'Name', the SQLFilter just stops working and i don't get any more messages.

What am I doing wrong?

Upvotes: 1

Views: 626

Answers (1)

Sean Feldman
Sean Feldman

Reputation: 25994

That's an expected behavior. The topic you create (CreatedMessages) is created once with the filter(s) and stored as-is. I.e. it's static. Whenever you decide that the filter needs to be changed, you should update your subscription to have a new filter.

To update an existing subscription with a new filter, you could look into NamespaceManager.GetRulesAsync(string, string) to get the existing rule(s) for your subscription and then remove the old one using SubscriptionClient.RemoveRuleAsync(String).

Upvotes: 1

Related Questions