BackgroundQueue is a simple way to queue background Tasks in ASP.Net Core and in .Net in general. You can download BackgroundQueue either from the Nuget Package Manager or from the official nuget.org website.
This package brings two useful queues with it, which both operate in the Background, that means that they do not block the current Thread. With these queues you can enqueue Tasks
or Tickets
which provide even more control. They are also fully thread save and come with a handy IServiceCollection
extension.
First up, you'll need to download the BackgroundQueue
nuget package from one of the sources named above. After that you need to decide which one of the queues you need.
-
In your
ConfigureServices
method you want to addAddBackgroundTaskQueue
orAddBackgroundResultQueue
depending on your needs.public void ConfigureServices(IServiceCollection services) { [...] services.AddBackgroundTaskQueue(); //Or services.AddBackgroundResultQueue(); }
-
In your Controller constructor you need to request the queue implementation of your needs, e.g.
IBackgroundTaskQueue
/IBackgroundResultQueue
.This would look something like the following.public class HomeController : Controller { private readonly IBackgroundTaskQueue _taskQueue; //Or private readonly IBackgroundResultQueue _resultQueue; public DashboardController(IBackgroundTaskQueue taskQueue //Or IBackgroundResultQueue resultQueue) { _taskQueue = taskQueue; //Or _resultQueue = resultQueue; } }
-
In any Action you can now consume any of those queues and start enqueuing items.
public async Task<IActionResult> Index() { _taskQueue.Enqueue(async token => { await EmailSender.SendEmailAsync("Somone visited our website!"); }); // Will return immediately. //Or await _backgroundQueue.ProcessInQueueAsync(async token => { // I need to wait for any other items in this queue first! }); // Will continue after all other items, which are in front of it are processed. return View(); }
The BackgroundTaskQueue
is a queue which will enqueue items and immediately return to the current execution. You could use this queue for sending emails.
The BackgroundResultQueue
is a queue which will enqueue items and waits until the Task
/Ticket
finished processing including the result. You could use this queue, if you want requests to get processed step by step, but still want everything happen asynchronously.
If you feel like something is not working as intended or you are experiencing issues, feel free to create an issue. Also for feature requests just create an issue. For further information feel free to send me a mail to twenty@translucent.at
or message me on Discord 24_minutes#7496
.