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?