dotnet/aspnetcore · error · UnsupportedOperationException

Cannot handle type class

Error message

Cannot handle type class: {type.getClass()}

What it means

Utils.typeToClass maps a java.lang.reflect.Type to a concrete Class for casting. It handles Class, GenericArrayType, ParameterizedType, TypeVariable, and WildcardType; any other Type implementation is rejected because the protocol has no way to cast the decoded value to it. The rendered message is the actual runtime class name (the catalog template {type.getClass()} expands via string concatenation: 'Cannot handle type class: ' + type.getClass()).

Solutions

  1. Provide a concrete Class or a standard ParameterizedType (e.g. via TypeReference/ParameterizedTypeReference) for the hub method signature.
  2. Avoid custom java.lang.reflect.Type implementations in hub contracts.
  3. Wrap the argument in a plain POJO whose field types are concrete.

Example fix

// before
hubConnection.on("M", (x) -> {}, customTypeImpl); // customTypeImpl instanceof MyType == true
// after - use a plain Class
hubConnection.on("M", (x) -> {}, MyPojo.class);
Defensive patterns

Strategy: type-guard

Validate before calling

static boolean isSupportedType(java.lang.reflect.Type t) {
    return t instanceof Class || t instanceof java.lang.reflect.GenericArrayType
        || t instanceof java.lang.reflect.ParameterizedType
        || t instanceof java.lang.reflect.TypeVariable
        || t instanceof java.lang.reflect.WildcardType;
}

Type guard

static boolean isSupportedType(java.lang.reflect.Type t) {
    return t instanceof Class || t instanceof java.lang.reflect.GenericArrayType
        || t instanceof java.lang.reflect.ParameterizedType
        || t instanceof java.lang.reflect.TypeVariable
        || t instanceof java.lang.reflect.WildcardType;
}

Try / catch

try {
    Class<?> c = Utils.typeToClass(type);
} catch (UnsupportedOperationException ex) {
    // replace the exotic Type with a concrete Class before binding
    throw new IllegalArgumentException("Use a concrete/ParameterizedType, got " + type.getClass(), ex);
}

Prevention

When it happens

Trigger: A hub method parameter or return type resolves to an exotic Type that is not one of the five handled subtypes - e.g. a custom Type implementation, or a synthetic/generic-bridge type produced by some reflection libraries (Utils.java:94-98).

Common situations: Using a third-party TypeResolver, a TypeToken library, or Kotlin/Scala reified generics that surface as non-standard Type implementations; registering handlers with generic capture that resolves to an odd Type.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/bf7480c651223cc1. Report an issue: GitHub.

Appendix: source

Thrown at src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/Utils.java:98

    public static Class<?> typeToClass(Type type) {
        if (type == null) {
            return null;
        }
        if (type instanceof Class) {
            return (Class<?>) type;
        } else if (type instanceof GenericArrayType) {
            // Instantiate an array of the same type as this type, then return its class
            return Array.newInstance(typeToClass(((GenericArrayType)type).getGenericComponentType()), 0).getClass();
        } else if (type instanceof ParameterizedType) {
            return typeToClass(((ParameterizedType) type).getRawType());
        } else if (type instanceof TypeVariable) {
            Type[] bounds = ((TypeVariable<?>) type).getBounds();
            return bounds.length == 0 ? Object.class : typeToClass(bounds[0]);
        } else if (type instanceof WildcardType) {
            Type[] bounds = ((WildcardType) type).getUpperBounds();
            return bounds.length == 0 ? Object.class : typeToClass(bounds[0]);
        } else { 
            throw new UnsupportedOperationException("Cannot handle type class: " + type.getClass());
        }
    }
}

View on GitHub (pinned to 3600ca084e)