microsoft/aspire · error · ArgumentNullException

Value cannot be null. (Parameter 'serviceProvider')

Error message

Value cannot be null. (Parameter 'serviceProvider')

What it means

RequiredCommandValidator runs required-command validations for resources and reports failures via interactions. Its constructor null-guards its three dependencies, and passing a null IServiceProvider for serviceProvider throws ArgumentNullException with parameter name 'serviceProvider'. The provider is required to resolve per-resource validation callbacks and their services.

Solutions

  1. Pass the host's service provider, e.g. host.Services or app.Services, when constructing RequiredCommandValidator.
  2. Prefer resolving RequiredCommandValidator from DI (it is registered by the hosting layer) instead of constructing it manually.
  3. In tests, build one with new ServiceCollection().AddLogging().BuildServiceProvider().

Example fix

// before
var validator = new RequiredCommandValidator(null!, interactionService, logger);
// after
var validator = new RequiredCommandValidator(host.Services, interactionService, logger);
Defensive patterns

Strategy: validation

Validate before calling

if (serviceProvider is null)
{
    throw new InvalidOperationException("Resolve RequiredCommandValidator via DI or pass host.Services.");
}

Type guard

bool CanBuildValidator(IServiceProvider? sp, IInteractionService? isvc, ILogger? logger) => sp is not null && isvc is not null && logger is not null;

Try / catch

try
{
    var validator = new RequiredCommandValidator(serviceProvider, interactionService, logger);
}
catch (ArgumentNullException ex) when (ex.ParamName == "serviceProvider")
{
    serviceProvider = host.Services; // recover by using the host provider
}

Prevention

When it happens

Trigger: Manually new-ing RequiredCommandValidator(null!, interactionService, logger) — typically in tests or custom host bootstrap code; a DI container yielding null for the serviceProvider registration argument.

Common situations: Unit tests constructing the validator by hand without a service provider; custom hosting setups that bypass the standard AddAspire/hosting extension wiring and forget to supply the app's service provider.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/85673a15a84545ef. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/RequiredCommandValidator.cs:31

/// <summary>
/// Default implementation of <see cref="IRequiredCommandValidator"/> that validates commands
/// are available on the local machine PATH and coalesces validations per command.
/// </summary>
internal sealed class RequiredCommandValidator : IRequiredCommandValidator, IDisposable
{
    private readonly IServiceProvider _serviceProvider;
    private readonly IInteractionService _interactionService;
    private readonly ILogger<RequiredCommandValidator> _logger;

    // Track validation state per command/callback pair to coalesce notifications and validation work.
    private readonly ConcurrentDictionary<CommandValidationCacheKey, CommandValidationState> _commandStates = new();

    public RequiredCommandValidator(
        IServiceProvider serviceProvider,
        IInteractionService interactionService,
        ILogger<RequiredCommandValidator> logger)
    {
        _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
        _interactionService = interactionService ?? throw new ArgumentNullException(nameof(interactionService));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    /// <summary>
    /// Disposes the command validation states, releasing their semaphores.
    /// </summary>
    public void Dispose()
    {
        foreach (var state in _commandStates.Values)
        {
            state.Dispose();
        }
        _commandStates.Clear();
    }

    /// <inheritdoc />
    public async Task<RequiredCommandValidationResult> ValidateAsync(

View on GitHub (pinned to 25830f84bd)