{"record":{"id":"97ff6f052b43f25b","repo":"microsoft/aspire","slug":"builder-gettype-fullname-build-returned-null","errorCode":null,"errorMessage":"'{builder.GetType().FullName}.Build()' returned null.","messagePattern":"'(.+?)\\.Build\\(\\)' returned null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.RemoteHost/Ats/ReferenceExpressionRef.cs","lineNumber":358,"sourceCode":"    {\n        var appendValueProviderMethod = builder.GetType().GetMethod(\n            \"AppendValueProvider\",\n            BindingFlags.Instance | BindingFlags.Public,\n            binder: null,\n            [typeof(object), typeof(string)],\n            modifiers: null)\n            ?? throw new InvalidOperationException($\"'{builder.GetType().FullName}' is missing AppendValueProvider(object, string).\");\n\n        appendValueProviderMethod.Invoke(builder, [valueProvider, null]);\n    }\n\n    private static object BuildReferenceExpression(object builder)\n    {\n        var buildMethod = builder.GetType().GetMethod(\"Build\", BindingFlags.Instance | BindingFlags.Public)\n            ?? throw new InvalidOperationException($\"'{builder.GetType().FullName}' is missing Build().\");\n\n        return buildMethod.Invoke(builder, null)\n            ?? throw new InvalidOperationException($\"'{builder.GetType().FullName}.Build()' returned null.\");\n    }\n\n    private static object CreateConditionalReferenceExpression(\n        object condition,\n        string matchValue,\n        object whenTrue,\n        object whenFalse)\n    {\n        var referenceExpressionType = GetRequiredHostingType(HostingTypeNames.ReferenceExpression, condition);\n        var valueProviderType = GetRequiredHostingType(HostingTypeNames.ValueProviderInterface, condition);\n\n        var createConditionalMethod = referenceExpressionType.GetMethod(\n            \"CreateConditional\",\n            BindingFlags.Public | BindingFlags.Static,\n            binder: null,\n            [valueProviderType, typeof(string), referenceExpressionType, referenceExpressionType],\n            modifiers: null)\n            ?? throw new InvalidOperationException($\"'{referenceExpressionType.FullName}' is missing CreateConditional(...).\");","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.RemoteHost/Ats/ReferenceExpressionRef.cs#L340-L376","documentation":"After reflectively invoking Builder.Build(), BuildReferenceExpression expects a non-null ReferenceExpression and throws this InvalidOperationException if the invocation returns null. This is a defensive invariant: Build() should always produce an expression for a well-formed builder.","triggerScenarios":"Build() invoked successfully but returned null — only realistically when the resolved Build() is an unexpected override/return shape from a mismatched assembly, or the wrong builder object was used.","commonSituations":"Custom/patched Aspire.Hosting build whose Build() returns null on empty builders; version mismatch causing the wrong Build overload to be selected.","solutions":["Ensure the builder has at least one AppendLiteral/AppendValueProvider part before calling Build (some versions return null for empty builders).","Align Aspire.Hosting package versions to a release where Build() is non-nullable ReferenceExpression.","Inspect the loaded Aspire.Hosting.dll version at runtime to rule out assembly conflicts.","If you cannot guarantee non-empty content, append an empty-string literal before Build()."],"exampleFix":"// before\nvar builder = CreateReferenceExpressionBuilder();\nvar expr = BuildReferenceExpression(builder); // no parts added\n\n// after\nvar builder = CreateReferenceExpressionBuilder();\nAppendLiteral(builder, string.Empty); // guarantees Build() has content\nvar expr = BuildReferenceExpression(builder);","handlingStrategy":"validation","validationCode":"// Ensure the builder has content before invoking Build().\nvar buildMethod = builder.GetType().GetMethod(\"Build\", BindingFlags.Instance | BindingFlags.Public)\n    ?? throw new NotSupportedException(\"No Build() method; wrong builder type.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var expr = BuildReferenceExpression(builder);\n}\ncatch (InvalidOperationException ex) when (ex.Message.EndsWith(\"Build()' returned null.\"))\n{\n    AppendLiteral(builder, string.Empty);\n    var expr = BuildReferenceExpression(builder); // retry with guaranteed content\n}","preventionTips":["Always append at least one literal or value provider before calling Build().","Verify the loaded Aspire.Hosting.dll version when Build unexpectedly returns null.","Treat null Build() results as an assembly-version red flag, not an expected case.","Add a regression test asserting Build() is non-null for typical expressions."],"tags":["reflection","reference-expression","null-result"],"backgroundTag":"internal-invariant-violation","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"}