microsoft/aspire · error · InvalidOperationException

Could not resolve runtime type

Error message

Could not resolve runtime type '{fullName}'.

What it means

GetRequiredHostingType resolves a required Aspire.Hosting runtime type (ReferenceExpression, IValueProvider, ReferenceExpression.Builder) by full name, first from the anchor object's assembly and then across loaded assemblies. If no loaded assembly exports a type with that exact full name, this InvalidOperationException is thrown. It signals the ATS remote host could not find the hosting type it needs, usually because no Aspire.Hosting assembly (or a wrong-version one) is loaded in the app domain.

Solutions

  1. Ensure Aspire.Hosting (and the referenced app model types) is restored and actually loaded by the apphost — check the apphost server log for LoaderExceptions Warnings.
  2. Align Aspire SDK/CLI version with the Aspire.* package versions and rebuild from a clean bin/obj.
  3. Verify the exact full type name still exists in the loaded Aspire.Hosting.dll (namespace moves across versions break the lookup).
  4. If assemblies are probed from custom paths, confirm the probe/manifest paths point at the restored Aspire.Hosting.dll.
Defensive patterns

Strategy: type-guard

Validate before calling

// resolve required hosting types before calling remote-host ATS APIs
var refExprType = Type.GetType("Aspire.Hosting.ApplicationModel.ReferenceExpression, Aspire.Hosting", throwOnError: false);
var valueProviderType = Type.GetType("Aspire.Hosting.ApplicationModel.IValueProvider, Aspire.Hosting", throwOnError: false);
if (refExprType is null || valueProviderType is null)
    throw new InvalidOperationException("Aspire.Hosting assembly is not loaded or is the wrong version.");

Type guard

static bool HostingTypesAvailable() =>
    Type.GetType("Aspire.Hosting.ApplicationModel.ReferenceExpression, Aspire.Hosting", throwOnError: false) is not null;

Try / catch

try { var result = ToConditionalReferenceExpression(args); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Could not resolve runtime type"))
{ log.LogError(ex, "Hosting type missing; check Aspire.Hosting loading"); throw; }

Prevention

When it happens

Trigger: CreateConditionalReferenceExpression (or CreateReferenceExpressionBuilder and friends) calls GetRequiredHostingType('Aspire.Hosting.ApplicationModel.ReferenceExpression' / '.IValueProvider' / '.ReferenceExpression+Builder') while the remote-host process has no Aspire.Hosting assembly loaded, or the loaded copy has renamed/moved the type (namespace changes across major versions).

Common situations: Remote-host binary/apphost server mismatched with integration assemblies on disk (the documented 'LoaderExceptions' scenario); running an app built against a different Aspire major version; Aspire.Hosting.dll failing to load due to missing dependencies so its types never appear.

Related errors


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

Appendix: source

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

    {
        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())
        {
            var type = assembly.GetType(fullName, throwOnError: false);
            if (type is not null)
            {
                return type;

View on GitHub (pinned to 25830f84bd)