{"record":{"id":"85673a15a84545ef","repo":"microsoft/aspire","slug":"value-cannot-be-null-parameter-serviceprovider","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'serviceProvider')","messagePattern":"Value cannot be null\\. \\(Parameter 'serviceProvider'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/RequiredCommandValidator.cs","lineNumber":31,"sourceCode":"/// <summary>\n/// Default implementation of <see cref=\"IRequiredCommandValidator\"/> that validates commands\n/// are available on the local machine PATH and coalesces validations per command.\n/// </summary>\ninternal sealed class RequiredCommandValidator : IRequiredCommandValidator, IDisposable\n{\n    private readonly IServiceProvider _serviceProvider;\n    private readonly IInteractionService _interactionService;\n    private readonly ILogger<RequiredCommandValidator> _logger;\n\n    // Track validation state per command/callback pair to coalesce notifications and validation work.\n    private readonly ConcurrentDictionary<CommandValidationCacheKey, CommandValidationState> _commandStates = new();\n\n    public RequiredCommandValidator(\n        IServiceProvider serviceProvider,\n        IInteractionService interactionService,\n        ILogger<RequiredCommandValidator> logger)\n    {\n        _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));\n        _interactionService = interactionService ?? throw new ArgumentNullException(nameof(interactionService));\n        _logger = logger ?? throw new ArgumentNullException(nameof(logger));\n    }\n\n    /// <summary>\n    /// Disposes the command validation states, releasing their semaphores.\n    /// </summary>\n    public void Dispose()\n    {\n        foreach (var state in _commandStates.Values)\n        {\n            state.Dispose();\n        }\n        _commandStates.Clear();\n    }\n\n    /// <inheritdoc />\n    public async Task<RequiredCommandValidationResult> ValidateAsync(","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/RequiredCommandValidator.cs#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the host's service provider, e.g. host.Services or app.Services, when constructing RequiredCommandValidator.","Prefer resolving RequiredCommandValidator from DI (it is registered by the hosting layer) instead of constructing it manually.","In tests, build one with new ServiceCollection().AddLogging().BuildServiceProvider()."],"exampleFix":"// before\nvar validator = new RequiredCommandValidator(null!, interactionService, logger);\n// after\nvar validator = new RequiredCommandValidator(host.Services, interactionService, logger);","handlingStrategy":"validation","validationCode":"if (serviceProvider is null)\n{\n    throw new InvalidOperationException(\"Resolve RequiredCommandValidator via DI or pass host.Services.\");\n}","typeGuard":"bool CanBuildValidator(IServiceProvider? sp, IInteractionService? isvc, ILogger? logger) => sp is not null && isvc is not null && logger is not null;","tryCatchPattern":"try\n{\n    var validator = new RequiredCommandValidator(serviceProvider, interactionService, logger);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"serviceProvider\")\n{\n    serviceProvider = host.Services; // recover by using the host provider\n}","preventionTips":["Prefer DI resolution over manual construction of RequiredCommandValidator.","Ensure the host is built (host.Services available) before constructing the validator.","Assert non-null dependencies at test setup time."],"tags":["null-argument","dependency-injection","validation"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}