Get message count from SubscriptionClient

subscriptionclient example
azure service bus get message count c#
new subscriptionclient
subscriptionclient registermessagehandler
subscriptionclient registermessagehandler example
subscriptionclient python
azure service bus queue example c#
subscriptionclient .net core

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;
}

Is there a way to go directly from the client to the SubscriptionDescription?

According to SubscriptionClient Class, it does not provide a direct way to get message count from a given SubscriptionClient object.

it throws a 401 Unauthorized error

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.

NamespaceManager nsm = new NamespaceManager(client.MessagingFactory.NamespaceEndpoints.First(), TokenProvider.CreateSharedAccessSignatureTokenProvider("{keyName}", "{SharedAccessKey}"));

var subscriptionDesc = nsm.GetSubscription(topicName, subscriptionName);

long messageCount = subscriptionDesc.MessageCount;

SubscriptionClient Class (Microsoft.ServiceBus.Messaging), Retrieve the count of messages held in queues and subscriptions by Get the message count at the subscription and verify that it's greater� The messages that are sent to a Service Bus topic are forwarded to subscriptions for that topic. So, the active message count on the topic itself is 0, as those messages have been successfully forwarded to the subscription. Get the message count at the subscription and verify that it's greater than 0.

You're constructing your NamespaceManager with incorrect data.

client.MessagingFactory.NamespaceEndpoints.First()

returns Azure Service Bus namespace URI, not a connection string that is needed.

Is there a way to go directly from the client to the SubscriptionDescription?

Not really. To get message count on an entity is a management operation that has to go through NamespaceManager. Client is run-time operations on messages, not management of entities. Also, you shouldn't be creating namespace manager every time. Once you have it, cache it and re-use.

Azure Service Bus - message count, Create subscription client SubscriptionClient mySubscriptionClient = factory. CreateSubscriptionClient(mySubscription); // Receive messages for (int count = 0 ;� Receive() Receives a message using the Microsoft.ServiceBus.Messaging.SubscriptionClient.InternalReceiver.. Receive(Int64) Receives a message using the Microsoft.ServiceBus.Messaging.SubscriptionClient.InternalReceiver.

As of now (November 2019) there is still no way to do this via SubscriptionClient.

Yet people offer workarounds in this topic so here is another one via ManagementClient:

public async static Task<long> GetSubscriptionMessageCountAsync(
    ManagementClient client,
    SubscriptionDescription subscription)
{
    var runtimeInfo = await client.GetSubscriptionRuntimeInfoAsync(
        subscription.TopicPath,
        subscription.SubscriptionName);

    return runtimeInfo.MessageCount;
}

SubscriptionClient Class (Microsoft.ServiceBus.Messaging), Multiple ways of retrieving the number of messages in a Service Bus Queue from Azure. and copy your connection string. Only 3 lines of codes are necessary for message count retrieval: Get the Service Bus Queue. DLQ message count It's not possible to obtain count of messages in the dead-letter queue at the topic level. That's because messages don't sit at the topic level unless Service Bus throws an internal error.

Retrieving the number of messages of a Service Bus Queue in .NET , I am looking for node.js code, many places on internet they have given in JAVA or c# but for count I cannot find MessageCount. Any help will be fine. Link to the� To get message count on an entity is a management operation that has to go through NamespaceManager. Client is run-time operations on messages, not management of entities. Also, you shouldn't be creating namespace manager every time. Once you have it, cache it and re-use.

Getting count of total messages in a subscription client that is taking , [Service Bus] Cannot receive more than 2047 messages in a receiver let messageCount= 0; console.time("receive"); let receiver = client. Run the program, and check the Azure portal: click the name of your topic in the namespace Overview window. The topic Essentials screen is displayed. In the subscription listed near the bottom of the window, notice that the Message Count value for the subscription is now 10.

[Service Bus] Subscription Client Receiver never returns from , is used internally by the QueueClient and SubscriptionClient classes for receiving messages. The receive and delete mode is the simplest way of receiving messages from a queue. Receive messages from the queue with a 10 second timeout. A receiving application could have a delivery count threshold where any� Dead letter messages can be inspected by azure service bus explorer.Every subscription has two parts (X,X) with subscription name in service bus explorer. First part represents active messages count in subscriptions and second part represents dead letter message count. we will see this in details with example.

Comments
  • I do something similar now, see my comment in the other answer.
  • Too bad. I ended up creating the NamespaceManager once and then call GetSubscription() every time I want to know the number of messages. The code is a bit messy, because I have to pass around the NamespaceManager.
  • You're not supposed to mix management operations with the messaging operations according to the library design. That is even more enforced with the new microsoft.azure.servicebus client.
  • Yeah, I'm not too happy with my implementation, but it gets the job done. Now I understand that things can get convoluted quickly. If I'm going deeper that route, I will clean it up and separate the two concerns.