Skip to content

Getting Started

Get NexJob running in under 2 minutes.


1. Install

dotnet add package NexJob

Choose a storage provider (or use the default InMemory):

dotnet add package NexJob.Postgres
dotnet add package NexJob.SqlServer
dotnet add package NexJob.Redis
dotnet add package NexJob.MongoDB

2. Configure

using NexJob;

var builder = WebApplication.CreateBuilder(args);

// Register NexJob with InMemory storage (default)
builder.Services.AddNexJob()
               .AddNexJobJobs(typeof(Program).Assembly);

var app = builder.Build();
app.Run();

Or with a persistent storage provider (for example PostgreSQL). Register the provider before AddNexJob() so it replaces the default InMemory storage, and call AddNexJob() only once:

using NexJob.Postgres;   // the storage extension methods live in each package's own namespace

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);

3. Define a Job

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

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

    public async Task ExecuteAsync(CancellationToken ct)
    {
        await _email.SendAsync("user@example.com", "Welcome!", ct);
    }
}

Or with input:

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!", ct);
    }
}

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

4. Enqueue and Run

var scheduler = app.Services.GetRequiredService<IScheduler>();

// Simple job (no input)
await scheduler.EnqueueAsync<SendWelcomeEmailJob>(cancellationToken: ct);

// Job with input
await scheduler.EnqueueAsync<SendWelcomeEmailJob, SendWelcomeEmailInput>(
    new SendWelcomeEmailInput("user@example.com", "John"),
    cancellationToken: ct);

That's it. The dispatcher will pick up the job and execute it automatically.


Full Minimal Example (Worker Service)

// Program.cs
using NexJob;

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

await host.StartAsync();

// Nothing runs until a job is enqueued
var scheduler = host.Services.GetRequiredService<IScheduler>();
await scheduler.EnqueueAsync<HelloJob>();

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

Run:

dotnet run

Output:

Hello at 2026-04-08T12:00:00Z


Runnable Reference Samples

If you prefer learning by inspecting a live, working project, the repository includes ready-to-run samples in samples/:


Next Steps