{"record":{"id":"1239eed3438023bb","repo":"microsoft/aspire","slug":"aspire-getvalue-is-only-available-on-server-returned","errorCode":null,"errorMessage":"aspire: GetValue is only available on server-returned ReferenceExpression instances","messagePattern":"aspire: GetValue is only available on server-returned ReferenceExpression instances","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Go/Resources/base.go","lineNumber":203,"sourceCode":"\t\t\t\t\"whenTrue\":   r.WhenTrue.ToJSON(),\n\t\t\t\t\"whenFalse\":  r.WhenFalse.ToJSON(),\n\t\t\t\t\"matchValue\": r.MatchValue,\n\t\t\t},\n\t\t}\n\t}\n\treturn map[string]any{\n\t\t\"$expr\": map[string]any{\n\t\t\t\"format\":         r.Format,\n\t\t\t\"valueProviders\": r.ValueProviders,\n\t\t},\n\t}\n}\n\n// GetValue resolves the expression to its string value on the server.\n// Only available on server-returned ReferenceExpression instances (handle mode).\nfunc (r *ReferenceExpression) GetValue(token *CancellationToken) (string, error) {\n\tif r.handle == nil || r.client == nil {\n\t\treturn \"\", errors.New(\"aspire: GetValue is only available on server-returned ReferenceExpression instances\")\n\t}\n\n\targs := map[string]any{\n\t\t\"context\": r.handle.ToJSON(),\n\t}\n\tctx := context.Background()\n\tif token != nil {\n\t\tctx = token.Context()\n\t\tif id := r.client.registerCancellation(token); id != \"\" {\n\t\t\targs[\"cancellationToken\"] = id\n\t\t}\n\t}\n\n\tresult, err := r.client.invokeCapability(ctx, \"Aspire.Hosting.ApplicationModel/getValue\", args)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif s, ok := result.(string); ok {","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Go/Resources/base.go#L185-L221","documentation":"In the Go client, ReferenceExpression.GetValue only works when the expression came from the server and carries a handle and client. Locally constructed ReferenceExpression values (parsed inline, returned by non-server APIs) have no handle, so GetValue returns the error \"aspire: GetValue is only available on server-returned ReferenceExpression instances\".","triggerScenarios":"Calling GetValue on a ReferenceExpression created locally (e.g. via a constructor/parser or a client-side helper) instead of one returned by a server call that attaches handle and client.","commonSituations":"Hand-building a ReferenceExpression from a string in tests or glue code; caching an expression then losing its client association after reconnect; mixing client-side formatting with server-side resolution.","solutions":["Obtain the ReferenceExpression from a server-returned API/capability call so handle and client are populated.","For local expressions, resolve/format the value client-side instead of calling GetValue.","Ensure the client connection used to produce the expression is still alive and associated with it."],"exampleFix":"// before\nexpr := &ReferenceExpression{Value: cs}\ns, err := expr.GetValue(ctx)\n// after\nexpr := server.GetConnectionString(ctx) // server-returned, has handle+client\ns, err := expr.GetValue(ctx)","handlingStrategy":"validation","validationCode":"if expr == nil || !expr.HasHandle() { // or check expr.handle/client per API\n    return fmt.Errorf(\"expression is not server-returned; GetValue unavailable\")\n}","typeGuard":"func isServerReturned(expr *ReferenceExpression) bool {\n    return expr != nil && expr.handle != nil && expr.client != nil\n}","tryCatchPattern":"s, err := expr.GetValue(ctx)\nif err != nil && strings.Contains(err.Error(), \"only available on server-returned\") {\n    s = expr.FormatClientSide() // local fallback\n}","preventionTips":["Only call GetValue on expressions returned by server capability calls.","Resolve locally-built expressions client-side.","Keep the originating client connection alive for the lifetime of the expression."],"tags":["go","reference-expression","handle","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}