TaskBatcher 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TaskBatcher --version 3.0.0
                    
NuGet\Install-Package TaskBatcher -Version 3.0.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="TaskBatcher" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TaskBatcher" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TaskBatcher" />
                    
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 TaskBatcher --version 3.0.0
                    
#r "nuget: TaskBatcher, 3.0.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 TaskBatcher@3.0.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=TaskBatcher&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=TaskBatcher&version=3.0.0
                    
Install as a Cake Tool

TaskBatcher

This package collects incoming requests and processes them in batches. It is particularly useful in scenarios where multiple requests can be consolidated into a single database call, significantly reducing overhead and improving application scalability.

Usage/Examples

1) Define input that must be processed in batch:

public record Input(int V);

2) Define result:

public record Result(int V);

3) Define your bulk processing method:

public async Task<Dictionary<Guid, Result>> MyMethod(Dictionary<Guid, Input> reqs) {
	// Result guids must match request guids!!!  
	return reqs.toDictionary(el => el.Key, el => new Result(V: el.V));
}

4) Add taskBatcher under some unique key:

builder.Services.AddTaskBatcher<Input, Result>(
	"myUniqueKey",
	(sp) =>
	{
		return async (Dictionary<Guid, Input> reqs) =>
		{
			using var scope = sp.CreateScope(); // Create scope:
			var myService = scope.ServiceProvider.GetRequiredService<IMyService>(); // Get your services:
            return await myService.MyMethod(reqs); // Return results:  
		};
	}, 
	new TaskBatcherOptions
	{
		BatchSize = 50,
		WaitForBatchFill = TimeSpan.FromMilliseconds(10),
		WorkerCount = 1,
	}
);

4) Inject the TaskBatcher into your service and pass the request:

public class MyService(
	[FromKeyedServices("myUniqueKey")] TaskBatcher<Input, Result> myMethodBatched
) : IMyService {
    public async Task<Result> Example(Input req)
	{
		return await myMethodBatched.Run(new Input { V = 1 });
	}
}

TaskBatcher Options

new TaskBatcherOptions
{
    BatchSize = 50, // How many requests to process at once
    WaitForBatchFill = TimeSpan.FromMilliseconds(10), // How long to wait in case batch is not filled
    WorkerCount = 1 // How many batches to run in parallel
}

Auto Configuration

To make TaskBatcher automatically find bulk methods you have to:

1) Register auto configuration with:

builder.Services.ConfigureTaskBatchers(typeof(Program).Assembly);

Note that it must be done after the services configuration are made.

2) Mark your bulk methods with attribute:

[TaskBatchOperation("myUniqueKey")]

this will automatically invokes AddTaskBatcher on the target method.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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
4.0.9 472 11/19/2025
3.0.0 423 11/18/2025
2.1.2 422 11/18/2025
2.0.2 305 11/13/2025
2.0.1 175 10/11/2025