0

I built a service that polls the queues and topics on our ASB every few minutes. The code has an intialization part which sets up the classes, and the part that runs on the cron job that calls some functions of the classes.

The problem I am facing is that when running this, I get the same count on every scheduled execution. Once I stop the service and rerun it, I would get updated numbers.

The solution I have is to reinitialize everything on every scheduled run. The problem now is that I need to add some functionality where this will not be an option.

Below is the code (for topics only) which I have tried to simplify as much as possible:

The initialization of the code looks like

var nsmgr = NamespaceManager.CreateFromConnectionString("conn string");
var topics = nsmgr.GetTopics();

foreach (var topic in topics)
{
    var subscriptions = nsmgr.GetSubscriptions(topic.Path);
    foreach (var subscription in subscriptions)
    {         
        someStaticListOfAlerts.Add(new Alert (subscription.Name,()=> (int)subscription.MessageCountDetails.ActiveMessageCount))      

    }
}

The code that is called every few mins is

foreach (Alert a in someStaticListOfAlerts)
{
    a.Refresh();
}

A simplification of the alert class

public class Alert {
    public Alert (string name, Func<int> count)
    {
      // set in local vars
    }
    public void Refresh()
    {
      Console.Write ($"{_name} - {_count()}");
    }
}

Is this expected behaviour of the SubscriptionDescription class? I could not find much on MSDN in terms of refreshing the SubscriptionDescription or subscription and that leads me to believe that every time you call MessageDetails.Count it should return the most up to date number.

What am I doing wrong?

biso
  • 590
  • 1
  • 4
  • 13
  • In my experience the way you are fetching message counts should work, and should not be cached. This feels more like a problem with how this info is stored / reported on your end. Perhaps your static list somehow does not accept adds after the first pass, or old instances get captured somewhere. Hard to tell without seeing complete code, but try getting rid of that list and just print the counts off the subscription description right away a few times, for testing. – Slava Asipenko Apr 25 '17 at 12:49

1 Answers1

1

I had a similar problem with SubscriptionDescription MessageCount value non updated. What I found out is that the MessageCount property will not ask the actual message number to the service but it is initialized in the moment you create the SubscriptionDescription object.

For getting the updated message number you have to get the SubscriptionDescription again from the NamespaceManager.

NamespaceManager.GetSubscription("TopicName", "SubscriptionName").MessageCount;
Nardu
  • 334
  • 2
  • 14
  • Yes, that is true. I found out the same thing looking into the nuget of Azure service bus. I ended up doing what you suggested. :) – biso Apr 02 '18 at 20:02