microsoft/aspire · error · InvalidOperationException

Unexpected Task Hub name type

Error message

Unexpected Task Hub name type: {taskHubNameAnnotation.HubName.GetType().Name}

What it means

DurableTaskHubResource.TaskHubName converts the hub name stored in DurableTaskHubNameAnnotation into a ReferenceExpression. The annotation's HubName is expected to be either a string or a ParameterResource; any other stored type means the union type constraint was violated somewhere, so GetTaskHubName throws this InvalidOperationException as an internal invariant guard.

Solutions

  1. Pass either a string literal or a ParameterResource (e.g., builder.AddParameter(...).Resource) as the task hub name.
  2. Inspect the annotation construction site and fix the type of HubName.
  3. If using a builder API, rely on the [AspireUnion]-attributed overloads (WithTaskHubName) which validate the type at call time.

Example fix

// before
hubResource.Annotations.Add(new DurableTaskHubNameAnnotation(someExpression));

// after
var hubParam = builder.AddParameter("hub-name");
hubResource.WithTaskHubName(hubParam); // stores a ParameterResource
Defensive patterns

Strategy: type-guard

Validate before calling

if (hubName is not string and not ParameterResource) throw new InvalidOperationException($"Task hub name must be string or ParameterResource, got {hubName?.GetType().Name}");

Type guard

static bool IsValidHubName(object? v) => v is string or ParameterResource;

Try / catch

try { var expr = hubResource.TaskHubName; } catch (InvalidOperationException ex) when (ex.Message.StartsWith("Unexpected Task Hub name type")) { /* fix annotation producer */ }

Prevention

When it happens

Trigger: Attaching a DurableTaskHubNameAnnotation whose HubName is neither string nor ParameterResource (custom code or a future API misuse), then reading TaskHubName.

Common situations: Custom Durable Task extension code constructing the annotation with an unvalidated value; calling internal APIs with a ReferenceExpression or other type instead of the supported union members.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.Functions/DurableTask/DurableTaskHubResource.cs:52

    public ReferenceExpression TaskHubName => GetTaskHubName();

    /// <inheritdoc />
    void IResourceWithAzureFunctionsConfig.ApplyAzureFunctionsConfiguration(IDictionary<string, object> target, string connectionName)
    {
        // Injected to support Azure Functions listener initialization via the DTS storage provider.
        target["DURABLE_TASK_SCHEDULER_CONNECTION_STRING"] = Parent.ConnectionStringExpression;
        target["TASKHUB_NAME"] = TaskHubName;
    }

    private ReferenceExpression GetTaskHubName()
    {
        if (this.TryGetLastAnnotation<DurableTaskHubNameAnnotation>(out var taskHubNameAnnotation))
        {
            return taskHubNameAnnotation.HubName switch
            {
                ParameterResource parameter => ReferenceExpression.Create($"{parameter}"),
                string hubName => ReferenceExpression.Create($"{hubName}"),
                _ => throw new InvalidOperationException($"Unexpected Task Hub name type: {taskHubNameAnnotation.HubName.GetType().Name}")
            };
        }

        return ReferenceExpression.Create($"{Name}");
    }
}

View on GitHub (pinned to 25830f84bd)