{"record":{"id":"a307939629c46f8b","repo":"microsoft/aspire","slug":"union-value-is-of-type-value-getclass-getname-not-type","errorCode":null,"errorMessage":"Union value is of type {value.getClass().getName()}, not {type.getName()}","messagePattern":"Union value is of type (.+?), not (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Java/Resources/Base.java","lineNumber":84,"sourceCode":"\n    public static AspireUnion fromValue(Object value) {\n        return of(value);\n    }\n\n    public Object getValue() {\n        return value;\n    }\n\n    public boolean is(Class<?> type) {\n        return value != null && type.isInstance(value);\n    }\n\n    public <T> T getValueAs(Class<T> type) {\n        if (value == null) {\n            return null;\n        }\n        if (!type.isInstance(value)) {\n            throw new IllegalStateException(\"Union value is of type \" + value.getClass().getName() + \", not \" + type.getName());\n        }\n        return type.cast(value);\n    }\n\n    @Override\n    public String toString() {\n        return \"AspireUnion{\" + value + \"}\";\n    }\n}\n\n@FunctionalInterface\ninterface AspireAction0 {\n    void invoke();\n}\n\n@FunctionalInterface\ninterface AspireAction1<T1> {\n    void invoke(T1 arg1);","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Java/Resources/Base.java#L66-L102","documentation":"During pipeline execution Aspire validates that every compute resource (e.g. containers/projects with deployment targets) is bound to exactly one compute environment when the app model contains more than one. With a single environment it can be inferred implicitly, but with multiple environments the mapping is ambiguous, so ValidateComputeEnvironmentBindings throws this InvalidOperationException naming the unbound resources and the competing environments.","triggerScenarios":"App model contains 2+ compute environments (e.g. AddAzureEnvironment plus another environment resource) and a compute resource builder never calls WithComputeEnvironment, so publishing cannot decide which environment owns the resource.","commonSituations":"Apps evolved from single-environment to multi-environment setups where older resources never got an explicit binding; copy-pasted resource definitions missing the WithComputeEnvironment call; code-generated models where the environment registration was added later.","solutions":["Call .WithComputeEnvironment(environment) on the resource builder for each flagged resource, passing the target environment resource.","Reduce the model to a single compute environment if one shared target is actually intended.","Inspect environmentNames in the message to confirm which environments exist, then assign each resource explicitly.","Run the publish/deploy pipeline after fixes; the validation re-runs on every execution."],"exampleFix":"// before\nvar redis = builder.AddRedis(\"cache\"); // ambiguous with multiple environments\n// after\nvar redis = builder.AddRedis(\"cache\")\n    .WithComputeEnvironment(productionEnvironment);","handlingStrategy":"validation","validationCode":"// C#: pre-check compute environment binding before publish/deploy\nvar envs = appModel.Resources.OfType<ComputeEnvironmentResource>().ToList();\nif (envs.Count > 1)\n{\n    var unbound = appModel.Resources\n        .Where(r => r.IsContainer() || r is ProjectResource)\n        .Where(r => r.Annotations.OfType<ComputeEnvironmentAnnotation>().Count() > 1)\n        .ToList();\n    if (unbound.Count > 0) throw new InvalidOperationException(\"Assign each compute resource to one environment via WithComputeEnvironment.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await pipeline.ExecuteAsync(model);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not assigned to a compute environment\"))\n{\n    logger.LogError(ex, \"Ambiguous compute environment binding: {Message}\", ex.Message);\n}","preventionTips":["Call WithComputeEnvironment explicitly on every compute resource as soon as the model has more than one environment.","Keep one environment per AppHost unless multi-targeting is deliberate.","Review resource builder extensions for missing environment bindings after refactors."],"tags":["pipeline","compute-environment","validation","aspire"],"backgroundTag":"missing-required-config","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"}