Dependency Injection

What AddEggEncoder Registers

services.AddEggEncoder() registers exactly one service: IMediaEncoderNativeEncoder (scoped). Inject it into a controller, a scoped service, or resolve it from an IServiceScope in a background worker.

Typical Registration

// Program.cs
builder.Services.AddEggEncoder();

Consuming IMediaEncoder

public class TrackController(IMediaEncoder mediaEncoder) : ControllerBase
{
    [HttpPost("probe")]
    public async Task<IActionResult> Probe(string filePath)
    {
        var result = await mediaEncoder.Probe(filePath);
        return Ok(result);
    }
}

Resolving From a Background Worker

In a non-request context (a cron task, a queue consumer), create a scope explicitly:

public class TranscodeWorker(IServiceProvider serviceProvider)
{
    public async Task RunAsync(string sourcePath, string destPath)
    {
        await using var scope = serviceProvider.CreateAsyncScope();
        var mediaEncoder = scope.ServiceProvider.GetRequiredService<IMediaEncoder>();

        await mediaEncoder.ConvertFile(sourcePath, destPath);
    }
}

Without a DI Container

Every implementation has a plain constructor and works standalone — see Getting Started.