The Shared State Monster got me again.
I had a .AddTransient class with a singleton/lazy loaded property called Updater. I was thinking that class got re-created each time so I added a lazy property similar to this
public int UserId { get { return _queue?.UserId ?? 0; } }
private Queue _queue;
private async Task<Queue> GetQueue(int queueId)
{
if (_queue != null) return _queue;
_queue = await _queueFacade.GetQueueById(queueId);
return _queue;
}
🐛 The bug report said the wrong user id was getting sent to the queue in later code. Hmmmm…. It works while debugging…. oh wait! it has the wrong number the second time!!
The Updater class was injected into the BackgroundService, that is only created on startup. Thus only one time creation of the other class. Thus, the _queue property wasn’t changed.
The transient class should just be functions (it is now) with the data passed in.
This is what Functional Programming is teaching me, but I forgot that message. Hopefully I’ll remember next time.
It reminds me of Gene Kim’s “who filled my cup?!?” in his introduction to Fabulous Fortunes, Fewer Failures, and Faster Fixes from Functional Fundamentals - Scott Havens
Please consider using Brave and adding me to your BAT payment ledger. Then you won't have to see ads! (when I get to $100 in Google Ads for a payout (I'm at $95.73!), I pledge to turn off ads)
Also check out my Resources Page for referrals that would help me.