DalSoft.Hosting.BackgroundQueue
1.1.1
See the version list below for details.
dotnet add package DalSoft.Hosting.BackgroundQueue --version 1.1.1
NuGet\Install-Package DalSoft.Hosting.BackgroundQueue -Version 1.1.1
<PackageReference Include="DalSoft.Hosting.BackgroundQueue" Version="1.1.1" />
paket add DalSoft.Hosting.BackgroundQueue --version 1.1.1
#r "nuget: DalSoft.Hosting.BackgroundQueue, 1.1.1"
// Install DalSoft.Hosting.BackgroundQueue as a Cake Addin #addin nuget:?package=DalSoft.Hosting.BackgroundQueue&version=1.1.1 // Install DalSoft.Hosting.BackgroundQueue as a Cake Tool #tool nuget:?package=DalSoft.Hosting.BackgroundQueue&version=1.1.1
If you find this repo / package useful all I ask is you please star it ⭐
Update August 2021 Although Microsoft.NET.Sdk.Worker works well, you end up with a lot of bolierplate 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). For me I need something simple akin to HostingEnvironment.QueueBackgroundWorkItem, so I will continue to support and improve this package.
DalSoft.Hosting.BackgroundQueue
This is used in production environments however, the test coverage isn't where it needs to be. Should you run into a problem please raise an issue.
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 good 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())
.
Supported Platforms
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.
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
PM> Install-Package DalSoft.Hosting.BackgroundQueue
In your ASP.NET Core Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddBackgroundQueue(maxConcurrentCount:1, millisecondsToWaitBeforePickingUpTask:1000,
onException:exception =>
{
});
}
This setups DalSoft.Hosting.BackgroundQueue using .NET Core's DI container. If you're using a different DI container, you need to register IBackgroundQueue and BackgroundQueueService as singletons.
maxConcurrentCount (optional) maxConcurrentCount is the number of Tasks allowed to run in the background concurrently. maxConcurrentCount defaults to 1.
millisecondsToWaitBeforePickingUpTask (optional) If the number of background Tasks exceeds the maxConcurrentCount then millisecondsToWaitBeforePickingUpTask is used to delay picking up Tasks until the current Task is completed. millisecondsToWaitBeforePickingUpTask defaults to 1000, setting millisecondsToWaitBeforePickingUpTask lower than 500 throws an exception.
onException (required)
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>
passed to onException. onException is called any time a Task throws an exception.
As you would expect exceptions only affect the Task causing the exception, all other Tasks are processed as normal.
Queuing a Background Task
To queue a background Task just add IBackgroundQueue
to your controller's constructor and call Enqueue
.
public EmailController(IBackgroundQueue backgroundQueue)
{
_backgroundQueue = backgroundQueue;
}
[HttpPost, Route("/")]
public IActionResult SendEmail([FromBody]emailRequest)
{
_backgroundQueue.Enqueue(async cancellationToken =>
{
await _smtp.SendMailAsync(emailRequest.From, emailRequest.To, request.Body, cancellationToken);
});
return Ok();
}
Thread Safety
DalSoft.Hosting.BackgroundQueue uses a ConcurrentQueue and interlocked operations so is completely thread safe, just watch out for Access to Modified Closure issues.
Standing on the Shoulders of Giants
DalSoft.Hosting.BackgroundQueue is inspired by and gives credit to:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Made background queue service run on timer. Made backwards compatiable so Background or IBackground can be injected. Last version to support netstandard2.0