Acta. Durable execution for .NET

learn actatake the first steps

How to use your own services in a handler

The class that holds the handler takes its services in its constructor, from the application's own container.

A how-to for Acta, durable execution for .NET. New to Acta? Run your first durable job in five minutes.

Checked every release learn/000-fundamentals/007-dependency-injection
Read as

1Register the service as you would for any other code

dependency-injection.csexcerpt
var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddSingleton<IEmailSender, ConsoleEmailSender>();

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<DependencyInjectionJobs>("dependency-injection");
});

2Take it in the constructor of the handler's class

Acta constructs a fresh handler instance per attempt, injecting constructor dependencies.

dependency-injection.csexcerpt
public sealed class WelcomeJob(IEmailSender sender)
{
    [Job("send-welcome-email")]
    public async Task Handle(SendWelcomeEmail input, CancellationToken ct) =>
        await sender.SendAsync(input.Email, $"Welcome, {input.Name}!", ct);
}

3Run it and check the output

From a clone of the Acta repository, with the .NET 10 SDK:

Terminal
dotnet run --project learn/000-fundamentals/007-dependency-injection
Its output must contain
[email] [email protected] subject="Welcome, Sam!"

Checked every release. Before each release, Acta Verify runs this program and checks that its output contains these lines.

This page shows the code. The rules it relies on, starting with the handler running again from its first line, are in the handler contract.

The whole program

Every file of learn/000-fundamentals/007-dependency-injection, as it is compiled.

UseLocalDatabase is a helper of the Acta repository, not of the packages. In your own app, register the database with UseSqlite, UsePostgres or UseSqlServer, as in how to run your first job.

dependency-injection.cs59 lines
dependency-injection.cs
// How to use your own services in a handler
// The class that holds the handler takes its services in its constructor, from the application's own container.
using Acta;
using Acta.Learn.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

// learn: 1. Register the service as you would for any other code
builder.Services.AddSingleton<IEmailSender, ConsoleEmailSender>();

// learn: end

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<DependencyInjectionJobs>("dependency-injection");
});

using var host = builder.Build();
await host.StartAsync();

var jobs = host.Services.GetRequiredService<IJobs>();

await jobs.EnqueueAsync(new SendWelcomeEmail("[email protected]", "Sam"));
Console.WriteLine("Enqueued. Worker is running - press Ctrl+C to stop.");

await host.WaitForShutdownAsync();
await host.StopAsync();

namespace Acta.Learn.DependencyInjection
{
    public sealed record SendWelcomeEmail(string Email, string Name);

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

    public sealed class ConsoleEmailSender : IEmailSender
    {
        public Task SendAsync(string to, string subject, CancellationToken ct)
        {
            Console.WriteLine($"[email] to={to} subject=\"{subject}\"");
            return Task.CompletedTask;
        }
    }

    // learn: 2. Take it in the constructor of the handler's class
    // Acta constructs a fresh handler instance per attempt, injecting constructor dependencies.
    public sealed class WelcomeJob(IEmailSender sender)
    {
        [Job("send-welcome-email")]
        public async Task Handle(SendWelcomeEmail input, CancellationToken ct) =>
            await sender.SendAsync(input.Email, $"Welcome, {input.Name}!", ct);
    }
    // learn: end
}

Where to go next

New to Acta?

Acta is durable execution for .NET: workflows and jobs that survive restarts, as a library inside your application with its durable state in your SQL database.