Loading

Dependency injection

When using Nullean.Argh.Hosting, DI integration is fully transparent. Register your handler and middleware types in the service collection and the generated code resolves them automatically. No manual ServiceProvider wiring needed.

public class DeployCommands(IDeployService deployer)
{
    public async Task<int> Run(string environment)
    {
        await deployer.DeployAsync(environment);
        return 0;
    }
}
		
builder.Services.AddScoped<IDeployService, DeployService>();
builder.Services.AddArgh(args, b => b.Map<DeployCommands>());
		
  1. The service must be registered in the DI container
  2. Handler type is registered and its public methods become commands
API Purpose
Map<T>() Register T as transient and add all its public methods as commands.
MapTransient<T>() / MapScoped<T>() / MapSingleton<T>() Same, with an explicit DI lifetime.
UseGlobalOptions<T>() Register T as the global options type and add it to DI.
UseMiddleware<TMiddleware>() Register middleware as transient.
UseMiddleware<TMiddleware>(lifetime) Register middleware with an explicit DI lifetime.
using Microsoft.Extensions.DependencyInjection;

builder.Services.AddArgh(args, b =>
{
    b.MapScoped<DeployCommands>();
    b.UseMiddleware<AuditMiddleware>(ServiceLifetime.Singleton);
    b.Map("ping", PingHandlers.Run);
    b.UseGlobalOptions<GlobalOptions>();
});
		
  1. Resolved per command invocation
  2. Single instance for the process
  3. Static method - no DI lifetime needed
Warning

For native AOT / trimming, register handler and middleware types explicitly in DI so required constructors are preserved.