{"record":{"id":"69021393027db652","repo":"microsoft/aspire","slug":"argumentnullexception-for-parameter-services-services-is","errorCode":null,"errorMessage":"ArgumentNullException for parameter 'services' (services is null).","messagePattern":"ArgumentNullException for parameter 'services' \\(services is null\\)\\.","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ContainerBuildOptionsCallbackAnnotation.cs","lineNumber":59,"sourceCode":"public sealed class ContainerBuildOptionsCallbackContext\n{\n    /// <summary>\n    /// Initializes a new instance of <see cref=\"ContainerBuildOptionsCallbackContext\"/>.\n    /// </summary>\n    /// <param name=\"resource\">The resource being built.</param>\n    /// <param name=\"services\">The service provider.</param>\n    /// <param name=\"logger\">The logger instance.</param>\n    /// <param name=\"cancellationToken\">The cancellation token.</param>\n    /// <param name=\"executionContext\">The distributed application execution context.</param>\n    public ContainerBuildOptionsCallbackContext(\n        IResource resource,\n        IServiceProvider services,\n        ILogger logger,\n        CancellationToken cancellationToken,\n        DistributedApplicationExecutionContext executionContext)\n    {\n        Resource = resource ?? throw new ArgumentNullException(nameof(resource));\n        Services = services ?? throw new ArgumentNullException(nameof(services));\n        Logger = logger ?? throw new ArgumentNullException(nameof(logger));\n        CancellationToken = cancellationToken;\n        ExecutionContext = executionContext ?? throw new ArgumentNullException(nameof(executionContext));\n    }\n\n    /// <summary>\n    /// Gets the resource being built.\n    /// </summary>\n    public IResource Resource { get; }\n\n    /// <summary>\n    /// Gets the service provider.\n    /// </summary>\n    public IServiceProvider Services { get; }\n\n    /// <summary>\n    /// Gets the logger instance.\n    /// </summary>","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ContainerBuildOptionsCallbackAnnotation.cs#L41-L77","documentation":"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.","triggerScenarios":"Constructing ContainerBuildOptionsCallbackContext with null for the services parameter (tests or custom host code).","commonSituations":"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.","solutions":["Pass the application's IServiceProvider (from DistributedApplicationExecutionContext or the host builder).","In tests, build one via new ServiceCollection().BuildServiceProvider().","Ensure the code path creating the context runs after the host's service provider exists."],"exampleFix":"// before\nvar ctx = new ContainerBuildOptionsCallbackContext(resource, services!, logger, ct, execCtx); // services null\n\n// after\nvar services = new ServiceCollection().BuildServiceProvider();\nvar ctx = new ContainerBuildOptionsCallbackContext(resource, services, logger, ct, execCtx);","handlingStrategy":"validation","validationCode":"if (services is null) throw new InvalidOperationException(\"ServiceProvider must be initialized before creating ContainerBuildOptionsCallbackContext.\");","typeGuard":"bool HasProvider(IServiceProvider? sp) => sp is not null;","tryCatchPattern":"try { var ctx = new ContainerBuildOptionsCallbackContext(resource, services, logger, ct, execCtx); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"services\") { logger.LogError(ex, \"No service provider available; run inside the app host\"); }","preventionTips":["Obtain IServiceProvider from the execution context or host instead of constructing one ad hoc.","In tests, always build a minimal ServiceProvider via ServiceCollection.","Ensure context creation happens after host initialization, not in static initializers."],"tags":["argument-null","dependency-injection","aspire-pipelines"],"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"}