{"record":{"id":"096023206b8f3662","repo":"microsoft/aspire","slug":"the-azure-deployment-state-for-aks-environment-name-contains-096023","errorCode":null,"errorMessage":"The Azure deployment state for AKS environment '{Name}' contains an invalid scope.","messagePattern":"The Azure deployment state for AKS environment '(.+?)' contains an invalid scope\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs","lineNumber":426,"sourceCode":"        // resource ID so a changed AppHost scope cannot redirect cleanup to another cluster or wait\n        // on provisioning that is not part of the destroy graph.\n        string subscriptionId;\n        string? resourceGroupName;\n        if (deploymentStateSection.Data[\"Scope\"] is not null)\n        {\n            string? scopedSubscription;\n            string? scopedResourceGroup;\n            try\n            {\n                var scopeJson = deploymentStateSection.Data[\"Scope\"]!.GetValue<string>();\n                var scope = JsonNode.Parse(scopeJson)?.AsObject()\n                    ?? throw new InvalidOperationException(\"The persisted scope is not a JSON object.\");\n                scopedSubscription = scope[\"subscription\"]?.GetValue<string>();\n                scopedResourceGroup = scope[\"resourceGroup\"]?.GetValue<string>();\n            }\n            catch (Exception ex) when (ex is not OperationCanceledException)\n            {\n                throw new InvalidOperationException(\n                    $\"The Azure deployment state for AKS environment '{Name}' contains an invalid scope.\",\n                    ex);\n            }\n\n            (subscriptionId, resourceGroupName) = await ResolveDeploymentScopeAsync(\n                scopedSubscription,\n                scopedResourceGroup,\n                context.Services,\n                context.CancellationToken).ConfigureAwait(false);\n        }\n        else\n        {\n            subscriptionId = clusterResourceId.SubscriptionId!;\n            resourceGroupName = clusterResourceId.ResourceGroupName!;\n        }\n\n        await GetAksCredentialsAsync(\n            context,","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Kubernetes/AzureKubernetesEnvironmentResource.AksPipeline.cs#L408-L444","documentation":"Wrapper thrown when reading or parsing the persisted scope from the AKS deployment state throws any non-cancellation exception. The original exception (e.g. missing Scope key, invalid JSON, wrong value kind) is preserved as InnerException.","triggerScenarios":"GetAksCredentialsForDestroyAsync catches a failure while reading deploymentStateSection.Data[\"Scope\"] or parsing it as a JSON object and rethrows with the environment name in the message.","commonSituations":"Scope output missing from deployment state (null-forgiving read throws), malformed JSON after manual edits, value is a JSON array/string instead of an object, state file from an older schema.","solutions":["Read the InnerException to identify the root cause (missing key vs malformed JSON)","Re-deploy the AKS environment to regenerate a valid deployment state including the Scope output","Validate the state file's Scope entry is a JSON object with 'subscription' and 'resourceGroup' string properties","Delete the stale deployment state and let the pipeline recreate it"],"exampleFix":"// diagnose with:\n// catch (InvalidOperationException ex)\n// {\n//     Console.WriteLine(ex.InnerException); // e.g. NullReferenceException on Scope\n// }\n// fix: redeploy so Scope is rewritten by BicepUtilities.SetScopeAsync","handlingStrategy":"try-catch","validationCode":"if (!state.Data.TryGetValue(\"Scope\", out var scopeRaw) || string.IsNullOrWhiteSpace(scopeRaw))\n    throw new InvalidOperationException(\"Scope missing or empty in deployment state.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    /* read/parse scope */\n}\ncatch (InvalidOperationException ex)\n{\n    // inspect ex.InnerException for the parse failure, then redeploy to regenerate state\n}","preventionTips":["Check InnerException first to find the root cause","Redeploy to regenerate state rather than patching files by hand"],"tags":["azure","aks","deployment-state","json"],"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-21T09:17:21.228Z"}