microsoft/aspire · error · DistributedApplicationException

Resource ' ' is assigned to compute environment ' ' but…

Error message

Resource '{resource.Name}' is assigned to compute environment '{targetName}' but binds Kubernetes persistent volume '{annotation.Volume.Name}' which belongs to environment '{volumeEnvironment.Name}'. A workload can only bind persistent volumes declared on its Kubernetes compute environment. Declare the volume on the workload's Kubernetes environment, or assign the workload to '{supportedTargetName}' with WithComputeEnvironment.

What it means

In publish mode, a workload assigned to a compute environment binds a Kubernetes persistent volume declared on a different Kubernetes environment. Workloads may only bind volumes declared on their own Kubernetes environment (or the environment that owns their compute environment), so validation throws DistributedApplicationException.

Solutions

  1. Declare the persistent volume on the workload's own Kubernetes environment.
  2. Reassign the workload with WithComputeEnvironment to the environment (or its owning compute environment) that declares the volume.
  3. Move the volume declaration into the target environment used by the workload.

Example fix

// before
var volume = clusterA.AddPersistentVolume("data", ...);
var svc = builder.AddProject<Projects.Api>("api").WithComputeEnvironment(clusterB).WithVolume(volume);
// after
var volume = clusterB.AddPersistentVolume("data", ...);
var svc = builder.AddProject<Projects.Api>("api").WithComputeEnvironment(clusterB).WithVolume(volume);
Defensive patterns

Strategy: validation

Validate before calling

if (volume.Parent != workloadEnvironment && volume.Parent.OwningComputeEnvironment != workloadEnvironment) throw new Exception("Volume belongs to a different Kubernetes environment.");

Try / catch

try { PublishAsync(); } catch (DistributedApplicationException ex) when (ex.Message.Contains("belongs to environment")) { /* re-declare volume on the correct environment */ }

Prevention

When it happens

Trigger: Calling WithComputeEnvironment to assign a resource to environment A while binding a persistent volume created via environment B, then publishing.

Common situations: Multi-cluster/multi-environment AppHosts where volumes and workloads are declared on different Kubernetes environments; copy-pasted volume wiring after adding a second cluster.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Kubernetes/KubernetesEnvironmentExtensions.cs:244

            ApplyRunModeContainerVolumeName(resource, annotation);
        }
    }

    private static void ValidatePublishModePersistentVolumeBindings(PersistentVolumeBinding[] bindings)
    {
        foreach (var (resource, annotation) in bindings)
        {
            var targetEnvironment = resource.GetComputeEnvironment();
            var volumeEnvironment = annotation.Volume.Parent;

            // AKS owns an inner Kubernetes environment, so a binding is valid when the workload
            // targets either the Kubernetes environment directly or its owning compute environment.
            if (targetEnvironment != volumeEnvironment &&
                targetEnvironment != volumeEnvironment.OwningComputeEnvironment)
            {
                var targetName = targetEnvironment?.Name ?? "<none>";
                var supportedTargetName = (volumeEnvironment.OwningComputeEnvironment ?? volumeEnvironment).Name;
                throw new DistributedApplicationException(
                    $"Resource '{resource.Name}' is assigned to compute environment '{targetName}' but binds " +
                    $"Kubernetes persistent volume '{annotation.Volume.Name}' which belongs to environment " +
                    $"'{volumeEnvironment.Name}'. A workload can only bind persistent volumes declared on its " +
                    $"Kubernetes compute environment. Declare the volume on the workload's Kubernetes environment, " +
                    $"or assign the workload to '{supportedTargetName}' with WithComputeEnvironment.");
            }
        }
    }

    private static void ApplyRunModeContainerVolumeName(
        IResource resource,
        KubernetesPersistentVolumeBindingAnnotation binding)
    {
        if (resource is not ContainerResource || binding.RunModeContainerVolumeName is not { } localVolumeName)
        {
            return;
        }

View on GitHub (pinned to 25830f84bd)