JeffCren
JeffCren

Reputation: 416

Monitoring Azure Service Bus Queue Depth from ASP.NET Core 2.0 Console App

I am trying to monitor both total queue depth and DeadLetterMessages queue depth from an ASP.NET Core 2.0 console application. I have seen references to using the NamespaceManager class, something like this:

var ns = NamespaceManager.CreateFromConnectionString(sbConnectionString);
var queue = ns.GetQueue(queueName);
var count = queue.MessageCount;
var deadletterMessagesCount = queue.MessageCountDetails.DeadletterMessageCount;

(from How do you get the count of dead letter messages in an Azure Service Bus queue?)

but the NamespaceManager is in Microsoft.ServiceBus, which is not compatible with ASP.NET Core.

Any suggestions on how to get the message count?

Upvotes: 0

Views: 1281

Answers (2)

r3verse
r3verse

Reputation: 1070

Use the Azure.Messaging.ServiceBus.Administration namespace in the Azure.Messaging.ServiceBus library.

Setup a new administration client like this:

var client = new ServiceBusAdministrationClient("connectionstring");

Then get the message count/depth like so:

        var queuesRuntimeProperties = client.GetQueuesRuntimePropertiesAsync().AsPages();
        await foreach (var queuePage in queuesRuntimeProperties)
        {
            foreach (Azure.Messaging.ServiceBus.Administration.QueueRuntimeProperties currentQueue in queuePage.Values)
            {
                dict.Add(currentQueue.Name, currentQueue.TotalMessageCount.ToString());
            }
        }

Upvotes: 7

Tom Sun
Tom Sun

Reputation: 24529

Any suggestions on how to get the message count?

As Mikhail mentioned that it is not supported to that with Microsoft.Azure.ServiceBus currently.

If you want to get the message count with Azure SDK on the .net core platform, I recommand that you could use the Microsoft.Azure.Management.Fluent to do that.

The following is the demo code.

 //auth file:c:\tom\azureCredential.txt
 var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"c:\tom\azureCredential.txt"); 
 var azure = Azure
             .Configure()
             .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
             .Authenticate(credentials)
             .WithDefaultSubscription();

 var serviceBus = azure.ServiceBusNamespaces.GetByResourceGroup("Resource group name", "servicebus namespace");
 var queue = serviceBus.Queues.GetByName("queueName");
 var activeMessageCount = queue.ActiveMessageCount;
 var deadletterCount = queue.DeadLetterMessageCount;

Note: queue.MessageCount is the number all of the message including the DeadLetterMessage.

How to create an Azure Active Directory application and assign application to role.

How to create the auth file please refer to auth file formats

subscription=########-####-####-####-############
client=########-####-####-####-############
tenant=########-####-####-####-############
key=XXXXXXXXXXXXXXXX
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
graphURL=https\://graph.windows.net/

Test result:

enter image description here

Upvotes: -1

Related Questions