GovUK.Dfe.CoreLibs.AsyncProcessing
0.1.0
Prefix Reserved
See the version list below for details.
dotnet add package GovUK.Dfe.CoreLibs.AsyncProcessing --version 0.1.0
NuGet\Install-Package GovUK.Dfe.CoreLibs.AsyncProcessing -Version 0.1.0
<PackageReference Include="GovUK.Dfe.CoreLibs.AsyncProcessing" Version="0.1.0" />
<PackageVersion Include="GovUK.Dfe.CoreLibs.AsyncProcessing" Version="0.1.0" />
<PackageReference Include="GovUK.Dfe.CoreLibs.AsyncProcessing" />
paket add GovUK.Dfe.CoreLibs.AsyncProcessing --version 0.1.0
#r "nuget: GovUK.Dfe.CoreLibs.AsyncProcessing, 0.1.0"
#:package GovUK.Dfe.CoreLibs.AsyncProcessing@0.1.0
#addin nuget:?package=GovUK.Dfe.CoreLibs.AsyncProcessing&version=0.1.0
#tool nuget:?package=GovUK.Dfe.CoreLibs.AsyncProcessing&version=0.1.0
GovUK.Dfe.CoreLibs.BackgroundService
This library provides a robust framework for implementing long-running background tasks in .NET applications. It simplifies the development of background services by offering reusable components that streamline task scheduling, execution, and error handling. Ideal for any project requiring background processing, it ensures reliability and scalability across different environments.
Installation
To install the GovUK.Dfe.CoreLibs.BackgroundService Library, use the following command in your .NET project:
dotnet add package GovUK.Dfe.CoreLibs.BackgroundService
Usage
Usage in a Command Handler
Service Registration: You use a background service factory to enqueue tasks. Register the factory in your
Program.cs:public void ConfigureServices(IServiceCollection services) { services.AddBackgroundService(); }Implementation in the Handler: You enqueue tasks using
IBackgroundServiceFactorydirectly inside a command handler, optionally you can pass in an event to be raised when the task is completed, as shown in your code:public class CreateReportCommandHandler : IRequestHandler<CreateReportCommand, bool> { private readonly IBackgroundServiceFactory _backgroundServiceFactory; public CreateReportCommandHandler(IBackgroundServiceFactory backgroundServiceFactory) { _backgroundServiceFactory = backgroundServiceFactory; } public Task<bool> Handle(CreateReportCommand request, CancellationToken cancellationToken) { var taskName = "Create_Report_Task1"; _backgroundServiceFactory.EnqueueTask( async () => await (new CreateReportExampleTask()).RunAsync(taskName), result => new CreateReportExampleTaskCompletedEvent(taskName, result) ); return Task.FromResult(true); } }Events: The background service triggers events when a task is completed. For example:
public class CreateReportExampleTaskCompletedEvent : IBackgroundServiceEvent { public string TaskName { get; } public string Message { get; } public CreateReportExampleTaskCompletedEvent(string taskName, string message) { TaskName = taskName; Message = message; } }Event Handlers: These events are processed by event handlers. Here's an example of how you handle task completion events:
public class SimpleTaskCompletedEventHandler : IBackgroundServiceEventHandler<CreateReportExampleTaskCompletedEvent> { public Task Handle(CreateReportExampleTaskCompletedEvent notification, CancellationToken cancellationToken) { Console.WriteLine($"Event received for Task: {notification.TaskName}, Message: {notification.Message}"); return Task.CompletedTask; } }
This setup allows you to enqueue tasks in the background, fire events when tasks complete, and handle those events using a custom event handler architecture.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- MediatR (>= 12.4.1)
- MediatR.Contracts (>= 2.0.1)
- Microsoft.Extensions.Hosting (>= 8.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- System.Text.Json (>= 8.0.5)
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 |
|---|---|---|
| 1.0.13 | 362 | 11/8/2025 |
| 1.0.13-prerelease-19 | 169 | 11/8/2025 |
| 1.0.12 | 10,170 | 9/8/2025 |
| 0.1.0 | 206 | 9/8/2025 |
| 0.1.0-prerelease-143 | 207 | 9/8/2025 |