Skip to content

Quickstart

NexJob needs three things to run: a package reference, a service registration, and a job class. In about five minutes you will have a job running in your own project.

Before you start

You need the .NET 8 SDK or newer and a project to add NexJob to. For a new one, dotnet new worker -n MyJobs creates a Worker Service, and dotnet new web -n MyJobs creates an ASP.NET Core app.

Supported runtimes

The packages target net8.0 and net10.0. NuGet picks the target that matches your application; nothing to configure. An application on .NET 9 runs the net8.0 target.

Runtime Package target Status
.NET 8 net8.0 Supported
.NET 10 net10.0 Supported

net8.0 is dropped in a later major version, not before .NET 8 has been out of support for 6 months or a dependency requires it. Adding a target is not a breaking change; removing one is.

Your first job

1. Install the package

dotnet add package NexJob

The core package includes the dispatcher, the scheduler and an InMemory storage provider, ready to use with no further configuration.

2. Register the services

Call AddNexJob() once, then scan your assembly so NexJob can discover your job classes.

// Program.cs
using NexJob;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddNexJob();
        services.AddNexJobJobs(typeof(Program).Assembly);
    })
    .Build();

await host.StartAsync();

// Step 4 goes here

await host.WaitForShutdownAsync();
// Program.cs
using NexJob;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNexJob()
                .AddNexJobJobs(typeof(Program).Assembly);

var app = builder.Build();

// Step 4 goes here

app.Run();

Tip

Call AddNexJob() exactly once. Calling it multiple times registers duplicate background services and causes undefined behavior.

3. Define a job

Implement IJob for work that needs no input. NexJob resolves the job from the DI container, so constructor injection works. Each execution gets its own scope, so scoped services such as DbContext are safe to inject: see scoped services.

// HelloJob.cs
public sealed class HelloJob : IJob
{
    public Task ExecuteAsync(CancellationToken ct)
    {
        Console.WriteLine($"Hello at {DateTimeOffset.UtcNow:O}");
        return Task.CompletedTask;
    }
}

4. Enqueue the job

Resolve IScheduler from DI and call EnqueueAsync. The dispatcher picks the job up right away in the same process, through the wake-up channel.

var scheduler = host.Services.GetRequiredService<IScheduler>();   // Worker Service
// var scheduler = app.Services.GetRequiredService<IScheduler>(); // ASP.NET Core

await scheduler.EnqueueAsync<HelloJob>();

5. Run it

dotnet run

You will see the job's output in the console:

Hello at 2026-10-11T13:05:29.7182279+00:00

The dispatcher runs as a hosted BackgroundService in your own process. There is no separate worker process or sidecar. To split the web app and the workers into different processes, see Web app and worker in separate processes.

Going further

A job with typed input

Implement IJob<T> when the job needs structured input, and use a record for the input: it serializes cleanly and is immutable. The job below sends an e-mail through a service you register in DI (here a stand-in that writes to the console).

public interface IEmailService
{
    Task SendAsync(string to, string subject, CancellationToken ct);
}

public sealed class ConsoleEmailService : IEmailService
{
    public Task SendAsync(string to, string subject, CancellationToken ct)
    {
        Console.WriteLine($"To: {to} | {subject}");
        return Task.CompletedTask;
    }
}

public sealed class SendWelcomeEmailJob : IJob<SendWelcomeEmailInput>
{
    private readonly IEmailService _email;

    public SendWelcomeEmailJob(IEmailService email) => _email = email;

    public async Task ExecuteAsync(SendWelcomeEmailInput input, CancellationToken ct)
        => await _email.SendAsync(input.Email, $"Welcome, {input.UserName}!", ct);
}

public sealed record SendWelcomeEmailInput(string Email, string UserName);

Register the service next to AddNexJob() (services.AddSingleton<IEmailService, ConsoleEmailService>();) and enqueue with the input:

await scheduler.EnqueueAsync<SendWelcomeEmailJob, SendWelcomeEmailInput>(
    new SendWelcomeEmailInput("user@example.com", "Jane"));

Expire a job that starts too late

Set a deadline and the job expires on its own if the workers are too busy to start it in time:

await scheduler.EnqueueAsync<SendWelcomeEmailJob, SendWelcomeEmailInput>(
    new SendWelcomeEmailInput("user@example.com", "Jane"),
    deadlineAfter: TimeSpan.FromMinutes(5));

Note

The deadline is checked before execution begins, not during. A job enqueued with deadlineAfter: TimeSpan.FromMinutes(5) that has not started within 5 minutes is marked Expired and never executes.

Keep jobs across restarts

InMemory storage is for development and tests: jobs are lost when the process stops. For production, add a persistent provider:

dotnet add package NexJob.Postgres    # or NexJob.SqlServer, NexJob.Redis, NexJob.MongoDB

Register the provider before AddNexJob() so it replaces the InMemory default:

using NexJob;
using NexJob.Postgres;

builder.Services.AddNexJobPostgres(
    "Host=localhost;Database=nexjob;Username=postgres;Password=secret");

builder.Services.AddNexJob(options =>
{
    options.Workers = 20;
    options.MaxAttempts = 5;
})
.AddNexJobJobs(typeof(Program).Assembly);

See Storage Providers for the other databases.

Runnable reference samples

Clone the repository and dotnet run any sample. Three good starting points:

Sample Focus Port
NexJob.Sample.MinimalApi Dashboard, dead-letter handler, deadline enforcement 5001
NexJob.Sample.WorkerService Headless worker with the standalone embedded dashboard 5005
NexJob.Sample.Reliability Retry, checkpoint, deadline, dead-letter, circuit breaker, job control, health 5011

The samples folder has the rest (PostgreSQL, RabbitMQ, Kafka, cloud triggers, recurring jobs and more) and a docker-compose.yml with a local test stack for PostgreSQL, Redis, RabbitMQ, Kafka, SQL Server and MongoDB.

Next steps

  • Mental Model

    Understand storage-first design, the job state machine, wake-up channel, and crash recovery.

  • Storage Providers

    Configure PostgreSQL, SQL Server, Redis, or MongoDB for production workloads.

  • Scheduling

    Enqueue, schedule with delay, set deadlines, and chain job continuations.

  • Retries and dead letter

    Configure exponential backoff and handle exhausted retries gracefully.

  • Dashboard

    See your jobs, nodes and failures in real time, in ASP.NET Core or a Worker Service.

  • Delivery Guarantees

    What each failure costs a job: crashes, shutdown, throttling, pauses and deadlines.