{"record":{"id":"9d693907a1aa3a9e","repo":"microsoft/aspire","slug":"failed-to-apply-configuration-to-container-cr-modelresource","errorCode":null,"errorMessage":"Failed to apply configuration to container {cr.ModelResource.Name}","messagePattern":"Failed to apply configuration to container (.+?)","errorType":"exception","errorClass":"FailedToApplyEnvironmentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/Dcp/ContainerCreator.cs","lineNumber":322,"sourceCode":"\n        await ApplyBuildArgumentsAsync(dcpContainer, cr.ModelResource, _executionContext, logger, cToken).ConfigureAwait(false);\n\n        var spec = dcpContainer.Spec;\n\n        spec.VolumeMounts = BuildContainerMounts(cr.ModelResource);\n\n        var (runArgs, failedToApplyRunArgs) = await BuildRunArgsAsync(logger, cr.ModelResource, cToken).ConfigureAwait(false);\n        if (failedToApplyRunArgs)\n        {\n            throw new FailedToApplyEnvironmentException();\n        }\n        spec.RunArgs = runArgs;\n\n        var (configuration, pemCertificates, createFiles) = await BuildContainerConfiguration(cr, logger, cToken).ConfigureAwait(false);\n\n        if (configuration.Exception is not null)\n        {\n            throw new FailedToApplyEnvironmentException($\"Failed to apply configuration to container {cr.ModelResource.Name}\", configuration.Exception);\n        }\n\n        // Environment callbacks can resolve proxyless endpoint ports and commit a fallback host port,\n        // so build ports afterward.\n        if (cr.ServicesProduced.Count > 0)\n        {\n            spec.Ports = BuildContainerPorts(cr);\n        }\n\n        var args = configuration.Arguments.ToList();\n        if (modelContainer is ContainerResource { ShellExecution: true })\n        {\n            spec.Args = [\"-c\", $\"{string.Join(' ', args.Select(a => a.Value))}\"];\n        }\n        else\n        {\n            spec.Args = args.Select(a => a.Value).ToList();\n        }","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/Dcp/ContainerCreator.cs#L304-L340","documentation":"After run args, Aspire builds the container configuration (env vars, endpoints, certs, files). If BuildContainerConfiguration records an exception, the creator throws FailedToApplyEnvironmentException wrapping it, named for the model resource, so configuration could not be applied to the container.","triggerScenarios":"Any environment-variable callback, endpoint callback, or configuration value provider for the container resource throws while BuildContainerConfiguration runs inside BuildAndCreateContainerAsync.","commonSituations":"Environment callbacks referencing endpoints not yet allocated, a referenced resource/parameter missing or null, custom IValueProvider throwing during GetValueAsync, certificate/file setup failures.","solutions":["Read the InnerException (configuration.Exception) to identify the failing callback or value provider","Fix the environment/endpoint callback so it handles missing values safely","Ensure any referenced parameters or connection strings resolve before container creation"],"exampleFix":"// before\n.WithEnvironment(\"CONN\", ctx => ctx.Resource.GetConnectionInfo()!.ConnectionString!) // NRE if null\n// after\n.WithEnvironment(\"CONN\", ctx => ctx.Resource.GetConnectionString() ?? \"\")","handlingStrategy":"try-catch","validationCode":"// Resolve all parameters/connection strings before building the resource\nforeach (var p in parameters) { var v = await p.ValueProvider.GetValueAsync(ct); if (v is null) throw new InvalidOperationException($\"Parameter {p.Name} unresolved\"); }","typeGuard":"if (resource.TryGetAnnotationsOfType<EnvironmentCallbackAnnotation>(out var cbs) && cbs.Count == 0) { /* nothing to apply */ }","tryCatchPattern":"try { await app.StartAsync(); } catch (FailedToApplyEnvironmentException ex) { logger.LogError(ex.InnerException, \"Configuration failed for container; check inner exception\"); }","preventionTips":["Always inspect the InnerException - the real cause is there","Ensure endpoints/ports referenced in env callbacks exist at that stage","Resolve parameter defaults and connection strings before startup"],"tags":["containers","configuration","environment"],"backgroundTag":"invalid-config-value","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"}