microsoft/aspire · error · InvalidOperationException

Cannot support a generic interface that doesn't have…

Error message

Cannot support a generic interface that doesn't have exactly 1 argument.

What it means

When generating Python classes, the code generator maps C# interfaces that the resource type implements to Python generic base classes, which requires each generic interface to have exactly one type argument (Python generics like Base["T"]). If a resource type implements a generic interface with zero or multiple type arguments, the generator cannot express it and throws this InvalidOperationException.

Solutions

  1. Change the resource/capability type to implement a single-generic-argument interface (e.g. wrap a two-parameter generic in a dedicated single-parameter interface) before generating Python code.
  2. If you own the ATS registration, register a non-generic or singly-generic view type for the resource instead of the raw multi-arg generic interface.
  3. Check which interface the generator iterates (the message follows the implementedInterfaces loop) and remove that interface from the type's implemented surface if it is not needed for Python.
  4. If this is a framework type you cannot change, file an issue asking the generator to add a mapping/skip rule for that interface.

Example fix

// before
class MyStore : IResource, IDictionary<string, string> { ... }

// after
class MyStore : IResource, IEnumerable<KeyValuePair<string, string>> { ... }  // single generic argument
Defensive patterns

Strategy: validation

Validate before calling

// before generating, verify each implemented generic interface has exactly 1 type arg
bool generatable = type.GetInterfaces()
  .Where(i => i.IsGenericType)
  .All(i => i.GetGenericArguments().Length == 1);

Type guard

function isSingleGenericInterface(t) { return t.genericTypeArguments != null && t.genericTypeArguments.length === 1; }

Try / catch

try {
  await generator.GenerateDistributedApplication(context);
} catch (InvalidOperationException ex) when (ex.Message.Contains("generic interface")) {
  // identify the offending interface and adjust the resource model
}

Prevention

When it happens

Trigger: Running the Python code generator (GenerateDistributedApplication) against a resource model where an ATS-registered type implements a generic interface whose GenericTypeArguments array is null or has a length other than 1.

Common situations: Registering a custom resource type that implements something like IDictionary<string, string> or a generic callback interface with 2 type parameters, then generating Python stubs for the app model; adding a new capability interface to the ATS surface without updating the generator.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.CodeGeneration.Python/AtsPythonCodeGenerator.cs:1556

            if (implementedInterfaces is { Count: > 0 })
            {
                // Remove interfaces that are already implemented by another interface in the list
                var transitivelyImplemented = new HashSet<string>(
                    implementedInterfaces
                        .SelectMany(i => i.ImplementedInterfaces ?? [])
                        .Select(i => i.TypeId),
                    StringComparer.Ordinal);
                implementedInterfaces = implementedInterfaces
                    .Where(i => !transitivelyImplemented.Contains(i.TypeId))
                    .ToList();

                foreach (var i in implementedInterfaces)
                {
                    if (i.ClrType!.IsGenericType)
                    {
                        if (i.ClrType.GenericTypeArguments == null || i.ClrType.GenericTypeArguments.Length != 1)
                        {
                            throw new InvalidOperationException("Cannot support a generic interface that doesn't have exactly 1 argument.");
                        }
                        var genericSubType = i.ClrType.GenericTypeArguments[0];
                        baseClass += $", {DeriveClassName(i)}[\"{DeriveClassName(genericSubType)}\"]";
                    }
                    else
                    {
                        baseClass += ", " + DeriveClassName(i);
                    }
                }
            }
        
            sb.AppendLine(CultureInfo.InvariantCulture, $"class {builder.BuilderClassName}({baseClass}):");
            sb.AppendLine(CultureInfo.InvariantCulture, $"    \"\"\"{builder.BuilderClassName} resource.\"\"\"");
            sb.AppendLine();
            sb.AppendLine("    def __repr__(self) -> str:");
            sb.AppendLine(CultureInfo.InvariantCulture, $"        return \"{builder.BuilderClassName}(handle={{self._handle.handle_id}})\"");
            sb.AppendLine();
            sbOptions.AppendLine(CultureInfo.InvariantCulture, $"class {builder.BuilderClassName}Kwargs({optionsBaseClass}, total=False):");

View on GitHub (pinned to 25830f84bd)