In our application (ASP.NET core) we have events that require computations and which results need to be persisted in a database (the objects are projects that need recalculations when a property changes, and then the financial results need to stored). These actions need to take place asynchronously in the background.
Right now we have implemented this with an in-memory queue and background service by implementing System.Threading.SemaphoreSlim
and Microsoft.Extensions.Hosting.BackgroundService
. What is great about this is that event is immediately triggered on queueing the workitem. What's not great about it is that the workitems are lost in the queue when the application is killed.
For a more robust solution we have migrated the queue to an Azure Storage Queue, in which we queue serialized workitem objects. The queue has worked well, and now we need to replace the BackgroundService
.
After reviewing the Microsoft examples for de-queuing the message queue, I feel that polling directly makes most sense to me. This is because for example, Azure Queue storage trigger for Azure Functions also polls the queue, just with a simple algorithm. For the systems responsiveness a 5 second polling interval would meet our requirement. As such I have planned to implement Microsoft.Extensions.Hosting.IHostedService
with a System.Threading.Timer
for each queue (about a dozen queues) and poll my Azure storage queue every 5 seconds.
Q: Is there a more common or obvious implementation of a service that dequeues the Azure storage message queue in an ASP.NET core application? Or is this a sound approach to how one would use the Azure storage queue?