bytedev
bytedev

Reputation: 9119

How to purge a Azure ServiceBus Topics Subscrption in C# (Microsoft.Azure.ServiceBus v3+)?

How can I (in C#) purge an Azure service bus's topic subscription using the Microsoft.Azure.ServiceBus package (version 3+)?

I am aware of SO question. But the selected answer seems to use ReceiveBatch() on SubscriptionClient which does not appear to be available anymore(?). SubscriptionClient only really seems to now have RegisterMessageHandler() to handle messages, however using this means my client would never know when the subsciption is actually empty.

Is there a clean way to purge a topic using C#?

Upvotes: 1

Views: 893

Answers (1)

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35144

You can use MessageReceiver class in similar manner:

IMessageReceiver messageReceiver = new MessageReceiver(
    namespaceConnectionString,
    EntityNameHelper.FormatSubscriptionPath(topicName, subscriptionName),
    ReceiveMode.ReceiveAndDelete);

int batchSize = 100;
var operationTimeout = TimeSpan.FromSeconds(3);

do
{
    var messages = await messageReceiver.ReceiveAsync(batchSize, operationTimeout);
    if (messages == null || messages.Count == 0) // Returns null if no message is found
    {
       break;
    }
}
while (true);

Note that only async API is now available.

Upvotes: 2

Related Questions