TaskBatcher 4.0.9

dotnet add package TaskBatcher --version 4.0.9
                    
NuGet\Install-Package TaskBatcher -Version 4.0.9
                    
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="4.0.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TaskBatcher" Version="4.0.9" />
                    
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 4.0.9
                    
#r "nuget: TaskBatcher, 4.0.9"
                    
#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@4.0.9
                    
#: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=4.0.9
                    
Install as a Cake Addin
#tool nuget:?package=TaskBatcher&version=4.0.9
                    
Install as a Cake Tool

TaskBatcher

TaskBatcher automatically collects incoming requests, groups them into batches, and processes them using a single bulk method. This reduces database load, number of round trips, and improves application throughput.

✨ Features

  • Automatic request batching
  • Auto-discovery of bulk methods via attribute
  • Strongly typed request & response models
  • Correlates requests/responses using Guid keys
  • Parallel batch workers
  • Zero boilerplate for using bulk methods
  • Auto-generated batch keys based on service + request/response types

🚀 Quick Start

1) Register TaskBatcher

Must be done after all services are registered.

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

2) Mark your bulk method

Use the [TaskBatchOperation] attribute

[TaskBatchOperation]

3) Implement the bulk method

Your bulk method must:

  • accept Dictionary<Guid, TInput>
  • return Dictionary<Guid, TResult>
  • preserve key order (keys from request → same keys in response)
[TaskBatchOperation]
public async Task<Dictionary<Guid, Result>> MyBulkMethod(Dictionary<Guid, Input> reqs)
{
    return reqs.ToDictionary(
        el => el.Key,
        el => new Result()
    );
}

4) Inject ITaskBatcher and run single request

public class MyService(ITaskBatcher taskBatcher) : IMyService
{
    public async Task<Result> Example(Input req, CancellationToken ct)
    {
        return await taskBatcher.RunAsync<Input, Result>(
            this, // TaskBatcher auto-maps to correct bulk method
            req,
            ct
        );
    }
}

TaskBatcher automatically discovers the appropriate bulk method based on:

  • the service type (this)
  • the input type (Input)
  • the result type (Result)

⚙️ TaskBatcher Options

new TaskBatcherOptions
{
    BatchSize = 50,		// max requests per batch
    WaitForBatchFill = TimeSpan.FromMilliseconds(10),		// how long to wait before execute bulk method
    WorkerCount = 1		// how many batches can run in parallel
};

📌 Bulk Method Requirements

A valid bulk method must:

  1. Be annotated with [TaskBatchOperation]
  2. Accept: Dictionary<Guid, TInput>
  3. Return: Task<Dictionary<Guid, TResult>>
  4. Response keys must match request keys
  5. The service containing the method must be registered in DI before calling ConfigureTaskBatchers

🧠 Complete Example

public class UserService(
    ITaskBatcher taskBatcher,
    IUserRepository repo
) : IUserService
{
    [TaskBatchOperation(batchSize: 500)]
    public async Task<Dictionary<Guid, UserRes>> BulkGetUsers(
        Dictionary<Guid, UserReq> reqs)
    {
        var ids = reqs.Values.Select(x => x.UserId).ToList();
        var users = await repo.GetUsersByIds(ids);

        return reqs.ToDictionary(
            x => x.Key,
            x => users[x.Value.UserId]
        );
    }

    public Task<UserRes> GetUser(UserReq req, CancellationToken ct)
        => taskBatcher.RunAsync<UserReq, UserRes>(this, req, ct);
}

🛠️ Minimal Setup

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IMyService, MyService>();
// must come after service registrations
builder.Services.ConfigureTaskBatchers(typeof(Program).Assembly);
var app = builder.Build();
app.Run();

❗ Troubleshooting

❌ Error: No TaskBatch operation registered for X Possible causes:

  • Service not registered before ConfigureTaskBatchers
  • Assembly passed to ConfigureTaskBatchers does not include your services Fix:
builder.Services.AddScoped<IMyService, MyService>();
builder.Services.ConfigureTaskBatchers(typeof(Program).Assembly);

❌ Error: response keys do not match request keys

Correct:

return reqs.ToDictionary(
    x => x.Key,
    x => new Result(...)
);

📄 License

MIT

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 422 11/18/2025
2.1.2 422 11/18/2025
2.0.2 305 11/13/2025
2.0.1 175 10/11/2025