GovUK.Dfe.CoreLibs.AsyncProcessing 0.1.0

Prefix Reserved
There is a newer version of this package available.
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
                    
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="GovUK.Dfe.CoreLibs.AsyncProcessing" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GovUK.Dfe.CoreLibs.AsyncProcessing" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="GovUK.Dfe.CoreLibs.AsyncProcessing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GovUK.Dfe.CoreLibs.AsyncProcessing --version 0.1.0
                    
#r "nuget: GovUK.Dfe.CoreLibs.AsyncProcessing, 0.1.0"
                    
#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.
#:package GovUK.Dfe.CoreLibs.AsyncProcessing@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GovUK.Dfe.CoreLibs.AsyncProcessing&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=GovUK.Dfe.CoreLibs.AsyncProcessing&version=0.1.0
                    
Install as a Cake Tool

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

  1. 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();
        }
    
  2. Implementation in the Handler: You enqueue tasks using IBackgroundServiceFactory directly 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);
        }
    }
    
  3. 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;
        }
    }
    
  4. 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 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. 
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
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