{"record":{"id":"1f5fb546e085ae38","repo":"microsoft/aspire","slug":"referenceexpressiontype-fullname-createconditional-returned","errorCode":null,"errorMessage":"'{referenceExpressionType.FullName}.CreateConditional(...)' returned null.","messagePattern":"'(.+?)\\.CreateConditional\\(\\.\\.\\.\\)' returned null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.RemoteHost/Ats/ReferenceExpressionRef.cs","lineNumber":379,"sourceCode":"    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(...).\");\n\n        return createConditionalMethod.Invoke(null, [condition, matchValue, whenTrue, whenFalse])\n            ?? throw new InvalidOperationException($\"'{referenceExpressionType.FullName}.CreateConditional(...)' returned null.\");\n    }\n\n    private static Type GetRequiredHostingType(string fullName, object? anchor = null) =>\n        FindHostingType(fullName, anchor) ??\n        throw new InvalidOperationException($\"Could not resolve runtime type '{fullName}'.\");\n\n    private static Type? FindHostingType(string fullName, object? anchor = null)\n    {\n        if (anchor is not null)\n        {\n            var anchoredType = anchor.GetType().Assembly.GetType(fullName, throwOnError: false);\n            if (anchoredType is not null)\n            {\n                return anchoredType;\n            }\n        }\n\n        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.RemoteHost/Ats/ReferenceExpressionRef.cs#L361-L397","documentation":"After finding CreateConditional via reflection, the remote host invokes it and expects a non-null ReferenceExpression back. If the method exists but returns null, this InvalidOperationException is thrown. ReferenceExpression.CreateConditional never legitimately returns null, so this indicates the invoked implementation misbehaved — typically a custom or patched ReferenceExpression type was substituted for the real one.","triggerScenarios":"ToConditionalReferenceExpression invokes the resolved CreateConditional method with (condition, matchValue, whenTrue, whenFalse) and the invoked member returns null. This requires the method to be found (see the 'missing CreateConditional' error) but produce a null result.","commonSituations":"A custom ATS type shim or mocked ReferenceExpression whose CreateConditional returns null; a bridged/duck-typed implementation in the remote host where the underlying factory silently failed.","solutions":["Ensure the real Aspire.Hosting ReferenceExpression type is loaded (check which assembly the resolved type comes from).","If a custom implementation is registered, fix it so CreateConditional always returns a constructed ReferenceExpression.","Update Aspire packages to a consistent version and rebuild to rule out patched/stale assemblies.","Inspect the apphost server log around the call to see whether an inner exception was swallowed by the factory."],"exampleFix":"// before: custom shim returns null on unsupported paths\npublic static ReferenceExpression CreateConditional(IValueProvider p, string m, ReferenceExpression t, ReferenceExpression f) => null;\n// after: always return a valid branch\npublic static ReferenceExpression CreateConditional(IValueProvider p, string m, ReferenceExpression t, ReferenceExpression f)\n    => string.Equals(p.GetValueAsync(null)?.ToString(), m, StringComparison.Ordinal) ? t : f;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"static bool ReturnsNonNull(Func<ReferenceExpression?> factory) => factory() is not null;","tryCatchPattern":"try { var expr = ToConditionalReferenceExpression(cond, match, t, f); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"returned null\"))\n{ log.LogError(ex, \"CreateConditional returned null; inspect loaded ReferenceExpression implementation\"); throw; }","preventionTips":["Never substitute custom ReferenceExpression implementations whose factories can return null.","Verify with unit tests that conditional reference creation returns a non-null expression.","Keep stock Aspire.Hosting assemblies; avoid patched binaries."],"tags":["reflection","null-return","aspire-hosting","internal-invariant"],"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"}