DalSoft.Hosting.BackgroundQueue 2.0.1

dotnet add package DalSoft.Hosting.BackgroundQueue --version 2.0.1                
NuGet\Install-Package DalSoft.Hosting.BackgroundQueue -Version 2.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DalSoft.Hosting.BackgroundQueue" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DalSoft.Hosting.BackgroundQueue --version 2.0.1                
#r "nuget: DalSoft.Hosting.BackgroundQueue, 2.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DalSoft.Hosting.BackgroundQueue as a Cake Addin
#addin nuget:?package=DalSoft.Hosting.BackgroundQueue&version=2.0.1

// Install DalSoft.Hosting.BackgroundQueue as a Cake Tool
#tool nuget:?package=DalSoft.Hosting.BackgroundQueue&version=2.0.1                

If you find this repo / package useful all I ask is you please star it ⭐

Do you or the company you work for benefit from the tools I build? <br /> If so please consider Becoming a Sponsor it would be greatly appreciated ❤️

DalSoft.Hosting.BackgroundQueue

DalSoft.Hosting.BackgroundQueue is a very lightweight .NET Core replacement for HostingEnvironment.QueueBackgroundWorkItem it has no extra dependencies!

For those of you that haven't used HostingEnvironment.QueueBackgroundWorkItem it was a simple way in .NET 4.5.2 to safely run a background task on a webhost, for example, sending an email when a user registers.

Yes there are loads of great options (hangfire, Azure Web Jobs/Functions) for doing this, but nothing in ASP.NET Core to replace the simplicity of the classic one-liner HostingEnvironment.QueueBackgroundWorkItem(cancellationToken => DoWork()).

Most backgrounder code examples don't work properly when injecting scoped services like EF (see FAQ). This use case is taken care of and battle tested in production environments.

For me, I needed something simple akin to HostingEnvironment.QueueBackgroundWorkItem, so I will continue to support and improve this package.

This package has over 345k downloads and is used in many production environments, but should you run into a problem, please raise an issue.

Supported Platforms

v2.0.0 DalSoft.Hosting.BackgroundQueue uses BackgroundService, and works with .NET 6.0 and above.

v1.1.1 DalSoft.Hosting.BackgroundQueue uses IHostedService and works with any .NET Core 2.0 or higher IWebHost i.e. a server that supports ASP.NET Core. v.1.1.1 doesn't support DI (you don't get the serviceScope parameter).

DalSoft.Hosting.BackgroundQueue also works with .NET Core's lighter-weight IHost - i.e. just services no ASP.NET Core, ideal for microservices.

Getting Started

dotnet add package DalSoft.Hosting.BackgroundQueue

In your ASP.NET Core Startup.cs:

builder.Services.AddBackgroundQueue
(
   maxConcurrentCount: 1, millisecondsToWaitBeforePickingUpTask: 1000,
   onException: (exception, serviceScope) =>
   {
       serviceScope.ServiceProvider.GetRequiredService<ILogger<Program>>()
           .Log(LogLevel.Error, exception, exception.Message);
   }
);

This setups DalSoft.Hosting.BackgroundQueue using .NET Core's DI container. If you're using a different DI container, you need to register BackgroundQueue, IBackgroundQueue and BackgroundQueueService as singletons.

onException (required) <br /> You are running tasks in the background on a different thread you need to know when an exception occurred. This is done using the Action<Exception, IServiceScopeFactory> parameter passed to onException. onException is called any time a Task throws an exception.

maxConcurrentCount (optional) <br /> maxConcurrentCount is the number of Tasks allowed to run in the background concurrently. maxConcurrentCount defaults to 1. Setting maxConcurrentCount lower than 1 throws an exception.

millisecondsToWaitBeforePickingUpTask (optional) <br /> millisecondsToWaitBeforePickingUpTask is the delay before a background Task is added to the queue - defaults to 10 milliseconds.
Setting millisecondsToWaitBeforePickingUpTask lower than 10 throws an exception. In most cases you shouldn't need to change this setting it's useful if you have to 'warm up' or need more throttling before hitting the maxConcurrentCount.

As you would expect exceptions only affect the Task causing the exception, all other Tasks are processed as normal. You can get your services from the IServiceScopeFactory parameter i.e. serviceScope.ServiceProvider.GetRequiredService<ILogger<Program>>().

Queuing a Background Task

To queue a background Task just add IBackgroundQueue to your controller's constructor and call Enqueue.

Using a controller:

public EmailController(IBackgroundQueue backgroundQueue)
{
   _backgroundQueue = backgroundQueue;
}

[HttpPost, Route("/")]
public IActionResult SendEmail([FromBody]emailRequest)
{
   _backgroundQueue.Enqueue(async (cancellationToken, serviceScope) =>
   {
       var smtp = serviceScope.ServiceProvider.GetRequiredService<ISmtp>()
       await smtp.SendMailAsync(emailRequest.From, emailRequest.To, request.Body, cancellationToken);
   });

   return Ok();
}

Equivalent code using minimal API

app.MapPost("/", (IBackgroundQueue backgroundQueue) =>
{
    backgroundQueue.Enqueue(async (cancellationToken, serviceScope) =>
    {
        var smtp = serviceScope.ServiceProvider.GetRequiredService<ISmtp>()
        await smtp.SendMailAsync(emailRequest.From, emailRequest.To, request.Body, cancellationToken);
    });
})

Note services are scoped to the Enqueue Task you provide i.e. per run, this is by design.

A fully working ASP.NET example targeting net8.0 can be found here.

FAQ

I'm getting a System.ObjectDisposedException

If you're getting: <br /> System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection...

You're getting your service from your controller instead of Enqueue (make your code like the examples above).

Thread Safety

DalSoft.Hosting.BackgroundQueue uses a ConcurrentQueue and interlocked operations so is completely thread safe, watch out for Access to Modified Closure issues.

Brief History

Update September 2024 Version 2.0 is here with full DI support, and support for minimal APIs. Version 2.0 API is fully backwards compatible with versions 1.x.x.

Update August 2021 Although Microsoft.NET.Sdk.Worker works well, you end up with a lot of boilerplate code and have to solve things like exception handling and concurrency. MS are leaving it up to the end user to decide how to implement (which makes sense rather than trying to implement every scenario).

Update December 2019 ASP.NET Core 3.1 finally supports background tasks using Microsoft.NET.Sdk.Worker.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 829 9/12/2024
2.0.0 349 9/5/2024
1.1.1 202 9/4/2024
1.0.4 364,595 2/2/2018
1.0.3 1,469 1/7/2018
1.0.2 1,067 1/6/2018
1.0.1 1,082 1/6/2018
1.0.0 1,694 1/1/2018

Reduced millisecondsToWaitBeforePickingUpTask to 10 milliseconds. Better handling if CreateAsyncScope ever fails.