microsoft/aspire · error · ArgumentNullException

ArgumentNullException for parameter 'services' (services is…

Error message

ArgumentNullException for parameter 'services' (services is null).

What it means

ContainerBuildOptionsCallbackContext's constructor validates that the IServiceProvider is not null. Services are how the callback resolves dependencies while configuring build options, so a null provider is rejected immediately with ArgumentNullException.

Solutions

  1. Pass the application's IServiceProvider (from DistributedApplicationExecutionContext or the host builder).
  2. In tests, build one via new ServiceCollection().BuildServiceProvider().
  3. Ensure the code path creating the context runs after the host's service provider exists.

Example fix

// before
var ctx = new ContainerBuildOptionsCallbackContext(resource, services!, logger, ct, execCtx); // services null

// after
var services = new ServiceCollection().BuildServiceProvider();
var ctx = new ContainerBuildOptionsCallbackContext(resource, services, logger, ct, execCtx);
Defensive patterns

Strategy: validation

Validate before calling

if (services is null) throw new InvalidOperationException("ServiceProvider must be initialized before creating ContainerBuildOptionsCallbackContext.");

Type guard

bool HasProvider(IServiceProvider? sp) => sp is not null;

Try / catch

try { var ctx = new ContainerBuildOptionsCallbackContext(resource, services, logger, ct, execCtx); }
catch (ArgumentNullException ex) when (ex.ParamName == "services") { logger.LogError(ex, "No service provider available; run inside the app host"); }

Prevention

When it happens

Trigger: Constructing ContainerBuildOptionsCallbackContext with null for the services parameter (tests or custom host code).

Common situations: Test setups that don't wire a minimal ServiceCollection/ServiceProvider; custom pipeline code executed outside the normal host where the app's IServiceProvider was never captured.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/ContainerBuildOptionsCallbackAnnotation.cs:59

public sealed class ContainerBuildOptionsCallbackContext
{
    /// <summary>
    /// Initializes a new instance of <see cref="ContainerBuildOptionsCallbackContext"/>.
    /// </summary>
    /// <param name="resource">The resource being built.</param>
    /// <param name="services">The service provider.</param>
    /// <param name="logger">The logger instance.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <param name="executionContext">The distributed application execution context.</param>
    public ContainerBuildOptionsCallbackContext(
        IResource resource,
        IServiceProvider services,
        ILogger logger,
        CancellationToken cancellationToken,
        DistributedApplicationExecutionContext executionContext)
    {
        Resource = resource ?? throw new ArgumentNullException(nameof(resource));
        Services = services ?? throw new ArgumentNullException(nameof(services));
        Logger = logger ?? throw new ArgumentNullException(nameof(logger));
        CancellationToken = cancellationToken;
        ExecutionContext = executionContext ?? throw new ArgumentNullException(nameof(executionContext));
    }

    /// <summary>
    /// Gets the resource being built.
    /// </summary>
    public IResource Resource { get; }

    /// <summary>
    /// Gets the service provider.
    /// </summary>
    public IServiceProvider Services { get; }

    /// <summary>
    /// Gets the logger instance.
    /// </summary>

View on GitHub (pinned to 25830f84bd)