microsoft/aspire · error · InvalidOperationException

' .CreateConditional(...)' returned null.

Error message

'{referenceExpressionType.FullName}.CreateConditional(...)' returned null.

What it means

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.

Solutions

  1. Ensure the real Aspire.Hosting ReferenceExpression type is loaded (check which assembly the resolved type comes from).
  2. If a custom implementation is registered, fix it so CreateConditional always returns a constructed ReferenceExpression.
  3. Update Aspire packages to a consistent version and rebuild to rule out patched/stale assemblies.
  4. Inspect the apphost server log around the call to see whether an inner exception was swallowed by the factory.

Example fix

// before: custom shim returns null on unsupported paths
public static ReferenceExpression CreateConditional(IValueProvider p, string m, ReferenceExpression t, ReferenceExpression f) => null;
// after: always return a valid branch
public static ReferenceExpression CreateConditional(IValueProvider p, string m, ReferenceExpression t, ReferenceExpression f)
    => string.Equals(p.GetValueAsync(null)?.ToString(), m, StringComparison.Ordinal) ? t : f;
Defensive patterns

Strategy: try-catch

Type guard

static bool ReturnsNonNull(Func<ReferenceExpression?> factory) => factory() is not null;

Try / catch

try { var expr = ToConditionalReferenceExpression(cond, match, t, f); }
catch (InvalidOperationException ex) when (ex.Message.Contains("returned null"))
{ log.LogError(ex, "CreateConditional returned null; inspect loaded ReferenceExpression implementation"); throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.RemoteHost/Ats/ReferenceExpressionRef.cs:379

    private static object CreateConditionalReferenceExpression(
        object condition,
        string matchValue,
        object whenTrue,
        object whenFalse)
    {
        var referenceExpressionType = GetRequiredHostingType(HostingTypeNames.ReferenceExpression, condition);
        var valueProviderType = GetRequiredHostingType(HostingTypeNames.ValueProviderInterface, condition);

        var createConditionalMethod = referenceExpressionType.GetMethod(
            "CreateConditional",
            BindingFlags.Public | BindingFlags.Static,
            binder: null,
            [valueProviderType, typeof(string), referenceExpressionType, referenceExpressionType],
            modifiers: null)
            ?? throw new InvalidOperationException($"'{referenceExpressionType.FullName}' is missing CreateConditional(...).");

        return createConditionalMethod.Invoke(null, [condition, matchValue, whenTrue, whenFalse])
            ?? throw new InvalidOperationException($"'{referenceExpressionType.FullName}.CreateConditional(...)' returned null.");
    }

    private static Type GetRequiredHostingType(string fullName, object? anchor = null) =>
        FindHostingType(fullName, anchor) ??
        throw new InvalidOperationException($"Could not resolve runtime type '{fullName}'.");

    private static Type? FindHostingType(string fullName, object? anchor = null)
    {
        if (anchor is not null)
        {
            var anchoredType = anchor.GetType().Assembly.GetType(fullName, throwOnError: false);
            if (anchoredType is not null)
            {
                return anchoredType;
            }
        }

        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())

View on GitHub (pinned to 25830f84bd)