Skip to main content

DISP002: Missing AutoRegister Attribute

PropertyValue
Diagnostic IDDISP002
TitleConsider adding [AutoRegister] attribute
CategoryExcalibur.Dispatch.Handlers
SeverityInfo
Enabled by defaultYes

Cause

A handler class could benefit from the [AutoRegister] attribute to enable automatic discovery and registration by the source generator, reducing manual DI configuration.

Example

The following code triggers DISP002:

// Info DISP002: Handler 'CreateOrderHandler' could benefit from [AutoRegister]
// attribute for automatic registration
public class CreateOrderHandler : IActionHandler<CreateOrder>
{
public Task HandleAsync(CreateOrder action, CancellationToken cancellationToken)
{
// ...
return Task.CompletedTask;
}
}

How to Fix

Add the [AutoRegister] attribute to enable automatic discovery:

[AutoRegister]
public class CreateOrderHandler : IActionHandler<CreateOrder>
{
public Task HandleAsync(CreateOrder action, CancellationToken cancellationToken)
{
// ...
return Task.CompletedTask;
}
}

You can customize the handler's service lifetime:

[AutoRegister(Lifetime = ServiceLifetime.Singleton)]
public class StatelessHandler : IActionHandler<PingCommand>
{
public Task HandleAsync(PingCommand action, CancellationToken cancellationToken)
=> Task.CompletedTask;
}

When to Suppress

Suppress this diagnostic if you prefer explicit manual registration or have a custom handler registration strategy.

See Also