简体   繁体   中英

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#?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM