For logging purposes, I want to get the message count of an Azure Service Bus subscription, given a subscription client. The only examples I found use the NamespaceManager
, but that seems a bit redundant to me since I already have a SubscriptionClient
. Is there a way to go directly from the client to the SubscriptionDescription
?
Fwiw, I tried using the detour via the name space manager, but I get a it throws a 401 Unauthorized error:
int GetMessageCount(SubscriptionClient client) {
NameSpaceManager nsm = new NamespaceManager(client.MessagingFactory.NamespaceEndpoints.First());
SubscriptionDescription desc = nsm.GetSubscription(client.TopicPath, client.Name); // <-- throws error
long numMsg = desc.MessageCount;
return numMsg;
}
According to SubscriptionClient Class, it does not provide a direct way to get message count from a given SubscriptionClient object.
The code
client.MessagingFactory.NamespaceEndpoints.First()
returns namespace endpoint, you initialize a new instance of the Microsoft.ServiceBus.NamespaceManager class with that service namespace URI base address, but you do not specify a credential that authorizes you to perform actions, so it returns 401 error when you do GetSubscription action. The following code works fine on my side, you can try it.