microsoft/aspire · error · InvalidOperationException

Failed to create ' '.

Error message

Failed to create '{builderType.FullName}'.

What it means

CreateReferenceExpressionBuilder reflects for the Aspire.Hosting ReferenceExpression.Builder type and instantiates it with Activator.CreateInstance. If creation yields null (which CreateInstance can only do for nullable value types, but the null-check is a defensive invariant), or the type cannot be instantiated, this InvalidOperationException is thrown.

Solutions

  1. Reference a matching Aspire.Hosting version compatible with the RemoteHost code (align package versions).
  2. Verify ReferenceExpression.Builder still has a public parameterless constructor in your referenced version.
  3. If publishing trimmed/AOT, preserve the type/constructor with [DynamicDependency] or a TrimmerRootDescriptor.
  4. Check loaded-assembly versions for Aspire.Hosting conflicts (fusion/AssemblyLoadContext log).

Example fix

// before (csproj)
<PackageReference Include="Aspire.Hosting" Version="8.0.0" />

// after
<PackageReference Include="Aspire.Hosting" Version="13.0.0" /> <!-- aligned with RemoteHost -->
Defensive patterns

Strategy: try-catch

Validate before calling

var builderType = typeof(ReferenceExpression).GetNestedType("Builder", BindingFlags.Public);
if (builderType?.GetConstructor(Type.EmptyTypes) is null)
    throw new NotSupportedException("ReferenceExpression.Builder with parameterless ctor is unavailable; check Aspire.Hosting version.");

Type guard

static bool CanCreateReferenceExpressionBuilder()
{
    var t = typeof(ReferenceExpression).GetNestedType("Builder", BindingFlags.Public);
    return t?.GetConstructor(Type.EmptyTypes) is not null && !t.IsAbstract;
}

Try / catch

try
{
    var exprRef = referenceExpressionRef.ToValueReferenceExpression(value);
}
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Failed to create"))
{
    logger.LogCritical(ex, "Aspire.Hosting assembly mismatch: ReferenceExpression.Builder could not be created.");
    throw new ApplicationException("Update Aspire.Hosting to the version matching the RemoteHost.", ex);
}

Prevention

When it happens

Trigger: ToValueReferenceExpression calls CreateReferenceExpressionBuilder; GetRequiredHostingType resolves the ReferenceExpressionBuilder type but Activator.CreateInstance returns null — typically because the type lacks a parameterless constructor or the wrong type was resolved.

Common situations: Aspire.Hosting version change renamed/restructured ReferenceExpression.Builder; trimming/AOT removed the constructor; wrong hosting assembly loaded (version conflict in bin).

Related errors


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

Appendix: source

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

            {
                // No closing brace - treat the rest as a literal
                parts.Add(format[start..]);
                break;
            }

            // Add the placeholder
            parts.Add(format[start..(end + 1)]);
            current = end + 1;
        }

        return [.. parts];
    }

    private static object CreateReferenceExpressionBuilder()
    {
        var builderType = GetRequiredHostingType(HostingTypeNames.ReferenceExpressionBuilder);
        return Activator.CreateInstance(builderType)
            ?? throw new InvalidOperationException($"Failed to create '{builderType.FullName}'.");
    }

    private static void AppendLiteral(object builder, string value)
    {
        var appendLiteralMethod = builder.GetType().GetMethod(
            "AppendLiteral",
            BindingFlags.Instance | BindingFlags.Public,
            binder: null,
            [typeof(string)],
            modifiers: null)
            ?? throw new InvalidOperationException($"'{builder.GetType().FullName}' is missing AppendLiteral(string).");

        appendLiteralMethod.Invoke(builder, [value]);
    }

    private static void AppendValueProvider(object builder, object valueProvider)
    {
        var appendValueProviderMethod = builder.GetType().GetMethod(

View on GitHub (pinned to 25830f84bd)